how to make an object readonly in runtime?

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
 
L

Lucas Tam

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?
 
S

Scott M.

If VB, declare the class members as Shared and the class itself as
NotInheritable. Set up the DataTable in the constructor.
 
J

Jon Skeet [C# MVP]

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.
 
S

Scott M.

"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.
 
K

Karl

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
 
B

Bob

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
 
L

Lloyd Sheen

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

Lloyd Sheen
 
J

Jay B. Harlow [MVP - Outlook]

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
 
J

Jon Skeet [C# MVP]

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.
 
B

Bob

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Bob,


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

Cheers,
 
W

William Stacey [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top