how to make an object readonly in runtime?

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

I have some data in a DataTable object that I'd like to put into a static
class for the entire app to use, kind of like a cahced global value. The
data in there should not change, but DataTable by default can be changed.
Is there an easy way to make it read only. I can probably create my own
wrapper class to accomplish this but hopefully there is a way to do this
directly on the DataTable.

Thanks in advance for any help
Bob
 
I can probably create my own
wrapper class to accomplish this but hopefully there is a way to do this
directly on the DataTable.

This might work, try declaring the table as constant and fill the datatable
in the constructor.

Or declare the datatable as private and provide your own methods for
accessing the datatable?
 
If VB, declare the class members as Shared and the class itself as
NotInheritable. Set up the DataTable in the constructor.
 
Scott M. said:
If VB, declare the class members as Shared and the class itself as
NotInheritable. Set up the DataTable in the constructor.

I don't see how that prevents the DataTable's data from changing.
 
"but DataTable by default can be changed"

He could use the constructor to determine what DataTable is in use. Or, he
could build a shared property that creates a DataTable.
 
Couldn't you return a copy() of the datatable? People would be able to make
changes to the copy of the datatable, but it wouldn't effect the underlaying
table.

Karl
 
Ha, I was thinking of this but was looking at the Clone(), which only clones
the schema. Should have eyeballed down a bit further. I'll run some test
on the speed of Copy() vs. creating a brand new DataTable and see if it has
an edge.

Thanks a lot
Bob
 
Perhaps you could capture one the "changing" events and then perform a
RejectChanges method to reverse all the changes.

Lloyd Sheen
 
Bob,
One thing I can think of to approximate a read-only DataTable would be to
handle the DataTable.RowChanging & DataTable.RowDeleting events and raise
exceptions to let the culprits know they shouldn't change the table.

You may be able to handle the events and keep the existing values, but I'm
not sure how well that will work.

Hope this helps
Jay
 
Scott M. said:
"but DataTable by default can be changed"

He could use the constructor to determine what DataTable is in use. Or, he
could build a shared property that creates a DataTable.

But when the DataTable has been created, it can be changed. That's the
point of his question.

Either he doesn't provide direct access to the DataTable in the first
place (which would no doubt mean duplicating all its read-only
functionality - argh) or he has to unfortunately take the hit that
DataTables are read-write.
 
I did some test and Copy() does meet my goal of caching the data to improve
speed. Rendering the same page takes anywhere from below 1 to 10
milliseconds vs. 10 to 30 milliseconds if creating the DataTable from an XML
string (Reading into DataSet). So it's a very good result.

Also thanks for all the other suggestions on handling events. I think that
would certianly be useful in some other situations.

Bob
 
Hi Bob,


Did you check if DataView with AllowEdit/Add/Delete disable do what you
want?

Cheers,
 
I might create an object/class with public properties for each column
instead of a DataTable. Then just clone that or make the Properties read
only. You could also include a GetDataTable() method that constructs a
DataTable from the object - so you have the flexability of Object, or
DataTable, or both depending on the needs.
 
Back
Top