|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Persist Changes made in class property to datasetI create a class with object properties. Then, iterate through the rows of a
dataset and equate select columns to the properties in the class as follows. foreach(DataRow r in this.ds.table) { Item item = new Item(); item.property1 = r["column1'"]; items.Add(item); } However, when I make change to the class property, the value does not change in the dataset. Is there anyway to equate these objects by reference and not value? Thanks, Duane Duane:
Do you mean that if you make a change to Item.property1 outside of this code, where you are setting it directly to the Row value, the underlying row value isn't changed? If so, the answer is going to depend on how the classes are created. From this code, it looks like everything is one way, you set the properties from the datarow but nothing handles that in reverse - is that correct? Since they are different objects, I don't think a simple reference is goign to work. More than likely, if I understand the issue correctly, you're going to need to raise an event in the properties of the class that writes to the corresponding row when it changes (actually, raise and event when a property changes and then have a handler write to the row) Show quote "Duane" <Du***@discussions.microsoft.com> wrote in message news:BFE76112-3EB2-4497-89A8-532D086BBA72@microsoft.com... >I create a class with object properties. Then, iterate through the rows of >a > dataset and equate select columns to the properties in the class as > follows. > > foreach(DataRow r in this.ds.table) > { > Item item = new Item(); > item.property1 = r["column1'"]; > items.Add(item); > } > > However, when I make change to the class property, the value does not > change > in the dataset. Is there anyway to equate these objects by reference and > not > value? > > Thanks, > > Duane > > Mr. Ryan,
Yes, the underlying row value does not change. I would like for the row value object and the class property to be the same object. Is this possible? Duane Show quote "W.G. Ryan - MVP" wrote: > Duane: > > Do you mean that if you make a change to Item.property1 outside of this > code, where you are setting it directly to the Row value, the underlying row > value isn't changed? If so, the answer is going to depend on how the classes > are created. > > From this code, it looks like everything is one way, you set the properties > from the datarow but nothing handles that in reverse - is that correct? > > Since they are different objects, I don't think a simple reference is goign > to work. More than likely, if I understand the issue correctly, you're going > to need to raise an event in the properties of the class that writes to the > corresponding row when it changes (actually, raise and event when a > property changes and then have a handler write to the row) > "Duane" <Du***@discussions.microsoft.com> wrote in message > news:BFE76112-3EB2-4497-89A8-532D086BBA72@microsoft.com... > >I create a class with object properties. Then, iterate through the rows of > >a > > dataset and equate select columns to the properties in the class as > > follows. > > > > foreach(DataRow r in this.ds.table) > > { > > Item item = new Item(); > > item.property1 = r["column1'"]; > > items.Add(item); > > } > > > > However, when I make change to the class property, the value does not > > change > > in the dataset. Is there anyway to equate these objects by reference and > > not > > value? > > > > Thanks, > > > > Duane > > > > > > > Even though strings are reference types, they are different in that strings
are immutable, so if you have a different string, you end up having a different object reference. So they end up often acting like value types. Because of this, there is no way for you to have both dataset and your object point to the same actual string, so that when the string changes both objects see the change. For integers, decimals, etc, the same would apply because they are value types. I am not sure why you are keeping all the data twice in memory - once in the dataset, and once in your own object collection. Stick with one or the other as your data source. Otherwise, you will need to make sure that anytime your object's property is change, that it either knows to change the dataset, or raises some event that you are handling to make sure the dataset's values stay in sync. Show quote "Duane" <Du***@discussions.microsoft.com> wrote in message news:9902F451-81F2-4CEE-9BCB-47B4ACBCA9F7@microsoft.com... > Mr. Ryan, > > Yes, the underlying row value does not change. I would like for the row > value object and the class property to be the same object. Is this > possible? > > Duane > > "W.G. Ryan - MVP" wrote: > >> Duane: >> >> Do you mean that if you make a change to Item.property1 outside of this >> code, where you are setting it directly to the Row value, the underlying >> row >> value isn't changed? If so, the answer is going to depend on how the >> classes >> are created. >> >> From this code, it looks like everything is one way, you set the >> properties >> from the datarow but nothing handles that in reverse - is that correct? >> >> Since they are different objects, I don't think a simple reference is >> goign >> to work. More than likely, if I understand the issue correctly, you're >> going >> to need to raise an event in the properties of the class that writes to >> the >> corresponding row when it changes (actually, raise and event when a >> property changes and then have a handler write to the row) >> "Duane" <Du***@discussions.microsoft.com> wrote in message >> news:BFE76112-3EB2-4497-89A8-532D086BBA72@microsoft.com... >> >I create a class with object properties. Then, iterate through the rows >> >of >> >a >> > dataset and equate select columns to the properties in the class as >> > follows. >> > >> > foreach(DataRow r in this.ds.table) >> > { >> > Item item = new Item(); >> > item.property1 = r["column1'"]; >> > items.Add(item); >> > } >> > >> > However, when I make change to the class property, the value does not >> > change >> > in the dataset. Is there anyway to equate these objects by reference >> > and >> > not >> > value? >> > >> > Thanks, >> > >> > Duane >> > >> > >> >> >> I agree with Marina's points below but depending on your layout, you could
have a DataRow property in the class. Set this to a given row whenver you needed an instance of the class - then just use the Row values for the property accessors instead of private variables - or in addition to private variables. I really would recommend against keeping two copies though b/c of synchronization issues and the code is going to be pretty complex. Show quote "Duane" <Du***@discussions.microsoft.com> wrote in message news:9902F451-81F2-4CEE-9BCB-47B4ACBCA9F7@microsoft.com... > Mr. Ryan, > > Yes, the underlying row value does not change. I would like for the row > value object and the class property to be the same object. Is this > possible? > > Duane > > "W.G. Ryan - MVP" wrote: > >> Duane: >> >> Do you mean that if you make a change to Item.property1 outside of this >> code, where you are setting it directly to the Row value, the underlying >> row >> value isn't changed? If so, the answer is going to depend on how the >> classes >> are created. >> >> From this code, it looks like everything is one way, you set the >> properties >> from the datarow but nothing handles that in reverse - is that correct? >> >> Since they are different objects, I don't think a simple reference is >> goign >> to work. More than likely, if I understand the issue correctly, you're >> going >> to need to raise an event in the properties of the class that writes to >> the >> corresponding row when it changes (actually, raise and event when a >> property changes and then have a handler write to the row) >> "Duane" <Du***@discussions.microsoft.com> wrote in message >> news:BFE76112-3EB2-4497-89A8-532D086BBA72@microsoft.com... >> >I create a class with object properties. Then, iterate through the rows >> >of >> >a >> > dataset and equate select columns to the properties in the class as >> > follows. >> > >> > foreach(DataRow r in this.ds.table) >> > { >> > Item item = new Item(); >> > item.property1 = r["column1'"]; >> > items.Add(item); >> > } >> > >> > However, when I make change to the class property, the value does not >> > change >> > in the dataset. Is there anyway to equate these objects by reference >> > and >> > not >> > value? >> > >> > Thanks, >> > >> > Duane >> > >> > >> >> >> |
|||||||||||||||||||||||