Re: MemberwiseClone() doesn't like arrays?



Easy - it has done a *shallow* clone, probably by a blit; this means
that it has, as promised, copied the member variable exactly; the
clone *also* contains a field that is a reference to the managed
heap - i.e. the same object. This is a common problem with using
shallow clone on an object whose fields are references.

You could try serializiation? This is the simplest approach to get a
deep-clone (assuming your data is serializable).

Or simply implement Clone() yourself, and manually clone the array
(giving the new instance the cloned array). Of course, if the array is
an array of reference-types (Customer, perhaps), then note that now
you have 2 arrays pointing to the same Customer objects, so
original.Data[0].Name = "Fred" will also update
newClone.Data[0].Name...

Do you see the problem?

Marc


.



Relevant Pages

  • Re: Instances effecting each other?
    ... //Please note that if x and y are reference types, you will want to clone ... it will definitely return a reference to a new object. ... So in that case how can i take an instance in an array and create a new ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: arraylist copy
    ... Then what is the difference between CopyTo and Clone? ... >reference, than they grew apart. ... >A copyto makes a copy of the values in a one dimensional array. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: arraylist copy
    ... Then what is the difference between CopyTo and Clone? ... > a reference, than they grew apart. ... > A copyto makes a copy of the values in a one dimensional array. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Instances effecting each other?
    ... Also make sure you will not be returning "this" reference in the Clone ... So in that case how can i take an instance in an array and create a new ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)