simple question on copying objects

  • Thread starter Thread starter Tom L
  • Start date Start date
T

Tom L

HOW?

It seems no matter what I do, it always does a reference to the originating
object. Even if I pass the object through a function, then create a "new"
one via Dim x as New someobject and then return x, it still sets a
reference - so then if I change a property in x it sets the original object.

The only solution I've found so far seems so tedious and ridiculous, which
is I have to copy each property individually, since copying a property
copies the value, and doesn't set a reference.
 
HOW?

It seems no matter what I do, it always does a reference to the originating
object. Even if I pass the object through a function, then create a "new"
one via Dim x as New someobject and then return x, it still sets a
reference - so then if I change a property in x it sets the original object.

The only solution I've found so far seems so tedious and ridiculous, which
is I have to copy each property individually, since copying a property
copies the value, and doesn't set a reference.

Have a look at the ICloneable interface, and Object.MemberwiseClone.

To be honest though, I rarely copy objects.
 
Maybe you have a better suggestion, but this is what I'm doing..

I have my main form which contains a variety of objects, in this case, an
Order object which has multiple Payment objects. I click a button on the
order form, which open a payment form. I pass the selected payment from the
order object to the payment form. I work on the payment object in the
payment form.. This should be seperate (not reference) to the order.payments
because if hte user hits cancel, then all changes are simply ignored, if the
user clicks ok, then I can copy the payment object back into the order
object. If there is a better way, I'm all ears.

thx
 
Maybe you have a better suggestion, but this is what I'm doing..

I have my main form which contains a variety of objects, in this case, an
Order object which has multiple Payment objects. I click a button on the
order form, which open a payment form. I pass the selected payment from the
order object to the payment form. I work on the payment object in the
payment form.. This should be seperate (not reference) to the order.payments
because if hte user hits cancel, then all changes are simply ignored, if the
user clicks ok, then I can copy the payment object back into the order
object. If there is a better way, I'm all ears.

Well, you could certainly implement ICloneable. Alternatively, you
might want to have a think about either using a DataRow or having
similar semantics, where there's a "current version", an "original
version" and a "proposed version" of the values. You can then accept
changed, end an edit, cancel an edit etc. You could model all that
yourself, or use a DataRow.
 
Here's what I do - and no tedium:

Typical Class:

[Serializable()]
public class CopyableObject
{
private int SomeProperty;
private in SomeOtherProperty;
//don't deep copy - copy object references separately from
CloneObject:
[NonSerialized()]
private SomeClass SomeObjectReference;
...
} //end class

And this utility method somewhere in the app:

internal static object CloneObject(object oObject)
{
//returns a separate object with all serializable
//properties set to those of oObject (the object class
//must have the "Serializable" attribute)

System.IO.MemoryStream oStream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
oFormatter = new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
oFormatter.Serialize(oStream, oObject);
oStream.Seek(0, System.IO.SeekOrigin.Begin);
return oFormatter.Deserialize(oStream);

}
 
Back
Top