How do I mark a DataTable as read-only

  • Thread starter Thread starter Philippe Bertrand
  • Start date Start date
P

Philippe Bertrand

I want to return a read-only DataTable so that I can cache it. Is it
possible?

Alternate suggestions (that don't waste tonnes of memory)?

Thanks,
Philippe
 
I want to supply users with a GetInfoTable() method that returns some
information that will not change during the lifetime of the object it is
called on. I want something like:

class MyClass
{
private DataTable _info;

public DataTable GetInfoTable()
{
if( _info == null ) {
_info = new DataTable();
// build the table up
}
return _info;
}
}

Since the user receives a reference, I don't want any accidental calls to
Clear() or Reset() or user invoked Dispose() (via using() directive, etc).
Obviously, it should delete the data when garbage collected - i.e.
Dispose( false ).

// user code
using( DataTable dt = myClass.GetInfoTable() ) {
dt.Clear(); // does nothing
dt.Reset(); // does nothing
// user can't add rows
} // not really disposed

Philippe
P.S. Does DataTable.Reset() go back to the state after EndInit() or
EndLoad() or does it completely clean out the DataTable ?
 
How does a read only property help? All that means is that the user can
change the reference (pointer) - once the user has a reference, he can
change the contents.

I'm following another api so I must return a DataTable. I can't hide it
(private with accessor methods). Deriving from DataTable isn't an option
because there are too many holes (Column collection can add/remove columns,
etc).

I guess it will have to be a feature request that you can lock/freeze a
DataTable.

Philippe
 
Back
Top