Persist Changes made in class property to dataset

G

Guest

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
 
W

W.G. Ryan - MVP

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)
 
G

Guest

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 said:
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 said:
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
 
M

Marina Levit [MVP]

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.

Duane said:
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 said:
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 said:
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
 
W

W.G. Ryan - MVP

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.
Duane said:
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 said:
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 said:
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
 

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

Top