Making copies inside static methods..please help!!

  • Thread starter Thread starter almurph
  • Start date Start date
A

almurph

Hi,


I'm working with a static classes and need to make a copy of an
object as I have to send it through a method that changes it and I
need both the original object and the newly altered object.

I'm doing something like as follows:

type originalObject;
type copyObject;


copyObject = originalObject;

SomeMethod(copyObject);


However I am noticing that the "originalObject" is being altered as
well. Any suggestions/comments/advice/code-samp-les that you may be
able to offer woudl be most appreciated.

Cheers,
Al.
 
I'm working with a static classes and need to make a copy of an
object as I have to send it through a method that changes it and I
need both the original object and the newly altered object.

I'm doing something like as follows:

type originalObject;
type copyObject;

copyObject = originalObject;

This does not copy the object; it copies the *reference* to the object,
so that now you have two references pointing to the same object. If the
object is modified through one of the references, the modifications will be
seen through the other refeernce.
Any suggestions/comments/advice/code-samp-les that you may be
able to offer woudl be most appreciated.

There is no universal way of copying an object. The way to copy it will
depend on its contents, and will take some effort of your part to write the
proper "copy code". In some cases, it may be enough to use the
MemberwiseClone method:
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx
The preceding article also mentions the difference between a shallow
copy and a deep copy.
 
    This does not copy the object; it copies the *reference* to the object,
so that now you have two references pointing to the same object. If the
object is modified through one of the references, the modifications will be
seen through the other refeernce.


    There is no universal way of copying an object. The way to copy it will
depend on its contents, and will take some effort of your part to write the
proper "copy code". In some cases, it may be enough to use the
MemberwiseClone method:http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone...
     The preceding article also mentions the difference between a shallow
copy and a deep copy.

Hi Alberto,

Thanks for this. Problem that I'm having is that my classes and
methods are static. The above MemberwiseClone has to be instance
based. Any ideas?

Thanks,
Al.
 
Thanks for this. Problem that I'm having is that my classes and
methods are static. The above MemberwiseClone has to be instance
based. Any ideas?

This doesn't make much sense. A method is never copied; when you talk
about copying an object what you mean is copying the data defined in the
class. However, if the class is static, all the data is static, meaning that
there can only be a copy of that data.

Note that this is inconsistent with your original sample code. You
wrote:
type originalObject;
type copyObject;

copyObject = originalObject;

In this case, "type" cannot be a static class. Both originalObject and
copyObject have to be _instances_ of "type" for this to work.
 
Hi Alberto,

   Thanks for this. Problem that I'm having is that my classes and
methods are static. The above MemberwiseClone has to be instance
based. Any ideas?

Thanks,
Al.

You have a contradiction there, If you have a static type then you
cannot create another instance of it.
 
Back
Top