J
Joe Duchtel
Hello -
I always thought that if I wanted a deep copy of an object, that I
needed to implement a copy constructor. However, I tried the
following code in VB.NET 2.0 and it worked just fine ...
Public Class cTestClassA
Protected mData As New ArrayList
Public Sub New(ByVal aNumber As Integer)
For i As Integer = 1 To aNumber
mData.Add(New cTestClassB(i.ToString))
Next
End Sub
Public Sub printData()
For i As Integer = 0 To mData.Count - 1
Console.WriteLine(CType(mData(i), cTestClassB).getData())
Next
End Sub
End Class
Public Class cTestClassB
Protected mName As String
Public Function getData() As String
Return mName
End Function
Public Sub New(ByVal aName As String)
mName = aName
End Sub
End Class
Sub Main(ByVal aCommandArguments() As String)
Dim lA As New cTestClassA(2)
lA.printData()
Dim lB As cTestClassA = lA ' <<<
lB.printData()
I thought that for the <<< assignment needed to be done via a copy
constructor but apparently not.
Can I rely on .NET to always create a deep copy? Am I missing
something?
Thanks,
Joe
I always thought that if I wanted a deep copy of an object, that I
needed to implement a copy constructor. However, I tried the
following code in VB.NET 2.0 and it worked just fine ...
Public Class cTestClassA
Protected mData As New ArrayList
Public Sub New(ByVal aNumber As Integer)
For i As Integer = 1 To aNumber
mData.Add(New cTestClassB(i.ToString))
Next
End Sub
Public Sub printData()
For i As Integer = 0 To mData.Count - 1
Console.WriteLine(CType(mData(i), cTestClassB).getData())
Next
End Sub
End Class
Public Class cTestClassB
Protected mName As String
Public Function getData() As String
Return mName
End Function
Public Sub New(ByVal aName As String)
mName = aName
End Sub
End Class
Sub Main(ByVal aCommandArguments() As String)
Dim lA As New cTestClassA(2)
lA.printData()
Dim lB As cTestClassA = lA ' <<<
lB.printData()
I thought that for the <<< assignment needed to be done via a copy
constructor but apparently not.
Can I rely on .NET to always create a deep copy? Am I missing
something?
Thanks,
Joe