Connecting Tech Pros Worldwide Forums | Help | Site Map

how to make an object readonly in runtime?

Bob
Guest
 
Posts: n/a
#1: Nov 18 '05
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



Lucas Tam
Guest
 
Posts: n/a
#2: Nov 18 '05

re: how to make an object readonly in runtime?


" Bob" <bobatkpmg@yahoo.com> wrote in news:OWgP0I9gEHA.3348
@TK2MSFTNGP12.phx.gbl:
[color=blue]
> I can probably create my own
> wrapper class to accomplish this but hopefully there is a way to do this
> directly on the DataTable.
>[/color]

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?



--
Lucas Tam (REMOVEnntp@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Scott M.
Guest
 
Posts: n/a
#3: Nov 18 '05

re: how to make an object readonly in runtime?


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


" Bob" <bobatkpmg@yahoo.com> wrote in message
news:OWgP0I9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=blue]
>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
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Nov 18 '05

re: how to make an object readonly in runtime?


Scott M. <s-mar@nospam.nospam> wrote:[color=blue]
> If VB, declare the class members as Shared and the class itself as
> NotInheritable. Set up the DataTable in the constructor.[/color]

I don't see how that prevents the DataTable's data from changing.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Scott M.
Guest
 
Posts: n/a
#5: Nov 18 '05

re: how to make an object readonly in runtime?


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

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1b8b3a10471c2c398b18e@msnews.microsoft.co m...[color=blue]
> Scott M. <s-mar@nospam.nospam> wrote:[color=green]
>> If VB, declare the class members as Shared and the class itself as
>> NotInheritable. Set up the DataTable in the constructor.[/color]
>
> I don't see how that prevents the DataTable's data from changing.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Karl
Guest
 
Posts: n/a
#6: Nov 18 '05

re: how to make an object readonly in runtime?


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

" Bob" <bobatkpmg@yahoo.com> wrote in message
news:OWgP0I9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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
>
>[/color]


Bob
Guest
 
Posts: n/a
#7: Nov 18 '05

re: how to make an object readonly in runtime?


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

"Karl" <none> wrote in message
news:%236Ob1z9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=blue]
> Couldn't you return a copy() of the datatable? People would be able to[/color]
make[color=blue]
> changes to the copy of the datatable, but it wouldn't effect the[/color]
underlaying[color=blue]
> table.
>
> Karl
>
> " Bob" <bobatkpmg@yahoo.com> wrote in message
> news:OWgP0I9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=green]
> > I have some data in a DataTable object that I'd like to put into a[/color][/color]
static[color=blue][color=green]
> > class for the entire app to use, kind of like a cahced global value.[/color][/color]
The[color=blue][color=green]
> > data in there should not change, but DataTable by default can be[/color][/color]
changed.[color=blue][color=green]
> > 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
> >
> >[/color]
>
>[/color]


Lloyd Sheen
Guest
 
Posts: n/a
#8: Nov 18 '05

re: how to make an object readonly in runtime?


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

Lloyd Sheen

" Bob" <bobatkpmg@yahoo.com> wrote in message
news:O7O5TE%23gEHA.536@TK2MSFTNGP11.phx.gbl...[color=blue]
> Ha, I was thinking of this but was looking at the Clone(), which only[/color]
clones[color=blue]
> 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[/color]
has[color=blue]
> an edge.
>
> Thanks a lot
> Bob
>
> "Karl" <none> wrote in message
> news:%236Ob1z9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=green]
> > Couldn't you return a copy() of the datatable? People would be able to[/color]
> make[color=green]
> > changes to the copy of the datatable, but it wouldn't effect the[/color]
> underlaying[color=green]
> > table.
> >
> > Karl
> >
> > " Bob" <bobatkpmg@yahoo.com> wrote in message
> > news:OWgP0I9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=darkred]
> > > I have some data in a DataTable object that I'd like to put into a[/color][/color]
> static[color=green][color=darkred]
> > > class for the entire app to use, kind of like a cahced global value.[/color][/color]
> The[color=green][color=darkred]
> > > data in there should not change, but DataTable by default can be[/color][/color]
> changed.[color=green][color=darkred]
> > > Is there an easy way to make it read only. I can probably create my[/color][/color][/color]
own[color=blue][color=green][color=darkred]
> > > wrapper class to accomplish this but hopefully there is a way to do[/color][/color][/color]
this[color=blue][color=green][color=darkred]
> > > directly on the DataTable.
> > >
> > > Thanks in advance for any help
> > > Bob
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#9: Nov 18 '05

re: how to make an object readonly in runtime?


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


" Bob" <bobatkpmg@yahoo.com> wrote in message
news:OWgP0I9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#10: Nov 18 '05

re: how to make an object readonly in runtime?


Scott M. <s-mar@nospam.nospam> wrote:[color=blue]
> "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.[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Bob
Guest
 
Posts: n/a
#11: Nov 18 '05

re: how to make an object readonly in runtime?


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

" Bob" <bobatkpmg@yahoo.com> wrote in message
news:O7O5TE%23gEHA.536@TK2MSFTNGP11.phx.gbl...[color=blue]
> Ha, I was thinking of this but was looking at the Clone(), which only[/color]
clones[color=blue]
> 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[/color]
has[color=blue]
> an edge.
>
> Thanks a lot
> Bob
>
> "Karl" <none> wrote in message
> news:%236Ob1z9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=green]
> > Couldn't you return a copy() of the datatable? People would be able to[/color]
> make[color=green]
> > changes to the copy of the datatable, but it wouldn't effect the[/color]
> underlaying[color=green]
> > table.
> >
> > Karl
> >
> > " Bob" <bobatkpmg@yahoo.com> wrote in message
> > news:OWgP0I9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=darkred]
> > > I have some data in a DataTable object that I'd like to put into a[/color][/color]
> static[color=green][color=darkred]
> > > class for the entire app to use, kind of like a cahced global value.[/color][/color]
> The[color=green][color=darkred]
> > > data in there should not change, but DataTable by default can be[/color][/color]
> changed.[color=green][color=darkred]
> > > Is there an easy way to make it read only. I can probably create my[/color][/color][/color]
own[color=blue][color=green][color=darkred]
> > > wrapper class to accomplish this but hopefully there is a way to do[/color][/color][/color]
this[color=blue][color=green][color=darkred]
> > > directly on the DataTable.
> > >
> > > Thanks in advance for any help
> > > Bob
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#12: Nov 18 '05

re: how to make an object readonly in runtime?


Hi Bob,


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

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

" Bob" <bobatkpmg@yahoo.com> wrote in message
news:OWgP0I9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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
>
>[/color]


William Stacey [MVP]
Guest
 
Posts: n/a
#13: Nov 18 '05

re: how to make an object readonly in runtime?


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.

--
William Stacey, MVP

" Bob" <bobatkpmg@yahoo.com> wrote in message
news:OWgP0I9gEHA.3348@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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
>
>[/color]

Closed Thread