J
Joel Merk
I have created a custom class with both value type members and reference type
members. I then have another custom class which inherits from a generic list
of my first class. This custom listneeds to support cloning:
Public Class RefClass
Public tcp As TcpClient
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable
Public Function Clone() As Object Implements System.ICloneable.Clone
Return MemberwiseClone()
End Function
Public Sub RemoveItem(ByVal item As RefClass)
Dim newList As RefClassList = Me.Clone
newList.Remove(item)
End Sub
End Class
Until now, MemberwiseClone() has served my purposes well. Because RefClass
contains a TcpClient, a deep clone is not desirable, because a Tcp Connection
is not duplicatable (logically). I need to clone because I need to modify the
collection while in a For Each loop; so creating a clone beforehand works for
me. When adding elements to the clone, I have no problems. However, when I
remove an object from the cloned list by referencem using RemoveItem(), the
associated item in the original list is set to Nothing and moved to the end
of the list. ReferenceEquals() applied to the original list and the clone
returns false, and the object has not been deleted (I added it to a separate
class, and it remained unchanged). I would like to know if this is a side
effect of MemberwiseClone, or perhaps Remove, and how I should fix it. I
tried several things, and one option that did not show this effect was:
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim newList As New UserList
newList.AddRange(Me.GetRange(0, Me.Count))
Return newList
End Function
I would like to know if there is a better way to work around this problem,
and I am also curious as to exactly why this happens. I was under the
impression that the member array of a list created through MemberwiseClone()
would not be linked to the original member list, but the references in the
new list would be the same.
Thanks!
members. I then have another custom class which inherits from a generic list
of my first class. This custom listneeds to support cloning:
Public Class RefClass
Public tcp As TcpClient
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable
Public Function Clone() As Object Implements System.ICloneable.Clone
Return MemberwiseClone()
End Function
Public Sub RemoveItem(ByVal item As RefClass)
Dim newList As RefClassList = Me.Clone
newList.Remove(item)
End Sub
End Class
Until now, MemberwiseClone() has served my purposes well. Because RefClass
contains a TcpClient, a deep clone is not desirable, because a Tcp Connection
is not duplicatable (logically). I need to clone because I need to modify the
collection while in a For Each loop; so creating a clone beforehand works for
me. When adding elements to the clone, I have no problems. However, when I
remove an object from the cloned list by referencem using RemoveItem(), the
associated item in the original list is set to Nothing and moved to the end
of the list. ReferenceEquals() applied to the original list and the clone
returns false, and the object has not been deleted (I added it to a separate
class, and it remained unchanged). I would like to know if this is a side
effect of MemberwiseClone, or perhaps Remove, and how I should fix it. I
tried several things, and one option that did not show this effect was:
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim newList As New UserList
newList.AddRange(Me.GetRange(0, Me.Count))
Return newList
End Function
I would like to know if there is a better way to work around this problem,
and I am also curious as to exactly why this happens. I was under the
impression that the member array of a list created through MemberwiseClone()
would not be linked to the original member list, but the references in the
new list would be the same.
Thanks!