views:

38

answers:

2

I'm trying to reset data in my object bound to a simple form (some textboxes and a couple of buttons).

I have 2 objects that were created separately, but have the same information. Object 1 is bound to a form using DataBinding. Object 2 is there to be able to reset Object 1 to original values. Whenever user edits data everything is fine, and Object 1 is updated with new values, while Object 2 stays the same. I'm having a problem with a scenario when user edits some data, and then clicks Reset Button which calls code that looks like this:

Object1 = Object2

Values get reset, but now when user edits data again, both Object1 and Object2 values are updated. And I can't reset anymore.

How can I reset Object1 values without making Object2 update-able at the same time? And can someone explain why Object2 becomes update-able after resetting?

Thank you.

+1  A: 

Object2 becomes bound as Object1 is now just a reference to Object2. to avoid this try copying just the properties of Object2 into Object1, or figure out some way to clone Object2 and then assign the clone to Object1.

Darko Z
+2  A: 

When you say Object1=Object2, you are simply making Object1 point to wherever object 2 is pointing. They are sharing the same reference and hence both are getting updated at the sametime.

My suggestion would be to deep clone. I don't know the complexity of your classes. This code was very handy for me.

http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx

So whenever you reset Object1, create a deep cloned object passing in Object2 and assign that to Object1.

SKG
That's what I thought, just needed to confirm. And DeepClone does work perfectly. Are there any performance concerns when using DeepClone that you are aware of? Thank you for your help.
chiefanov
Well you are basically serializing the source and deserializing, so theoretically the heavier the object this operation will take more time. I have played around with it using TypedDataSet Structures with multiple tables and associated data without any noticeable performance loss.
SKG
Sounds good. I'll play around with it. I don't have crazy large objects, so it shouldn't affect the performance much.
chiefanov