Clone Probes.

jerry_hokh

Active member
Joined
Mar 14, 2006
Messages
25
Programming Experience
1-3
Hi all,
It's would be appreciated if any body can help me this:

I have the following class:

Public Class cell
Implements ICloneable

Public name As String
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim c As cell = Me.MemberwiseClone
Return c
End Function
End Class


-----

Public Class main
Public Shared Sub main()


Dim arr As New ArrayList
For i As Integer = 0 To 10
Dim cell As New cell
arr.Add(cell)
Next
Dim arr2 As New ArrayList


arr2 = arr.clone
CType(arr(5), cell).name = "jerry"
End Sub
End Class


------
** the problem is I only change the 5th cell's name to "jerry" in arr, but in arr2's 5th cell'name is also change to "jerry" .I want arr2's 5th cell'name remain nothing

thanks alot for any help.

Jerry.
 
The Clone method of ArrayList is documented and is supposed to behave as you say, ie a shallow copy where the ArrayList object itself is cloned but the element references stay the same. This can be verified by checking if 'arr2 Is arr' (they are not same objects after clone).

The deep cloning you want can be done for instance with this 'simple' serialization method: http://www.vbdotnetforums.com/showthread.php?t=5802&highlight=cloning (you only need the CloneObject method)
 
I still got problem

the foremention way is just used for those class that you made, for my case, my child object has refferent to .Net object that can not be modify...

So it end up with the error : That .Net object has not yet mark serializable !!! :(

Can any body help me?
 
There are several ways to handle serialization of a class, the easiest is to mark the class with the Serializable attribute:
VB.NET:
<Serializable()> Public Class cell
 
Cant marks a dotnet's class as Serializable.

My child class in the array that i want to deep clone contains this event argument: System.Drawing.Printing.PrintPagEventArgs

But we cant mark PrintPagEventArgs class as Serializable because it cannot be modified.

So is there any way to deep clone my array?

Thanks a lot for any help....

Regards,
Jerry.
 
In your Cell class you can't mark String class Serializable either, but the Name member of type String you can... although you don't because this default behaviour, so you only mark those members included by default as NonSerialized when you don't want them to.
 
Back
Top