S
Simon Woods
Hi
I'm just beginning to learn VB.Net so if this seems dumb, please forgive me.
I have 2 objects and I want to make a deep copy of one into the other. I effectively want to clone an object into Me. (I'm also looking to learn about serialisation).
Public Class MyClass
Public Function Clone(AnObject as MyClass) as Integer
'ideally!!! make deep copy of all members of AnObject into Me
If CopyObject(AnObject, Me) = Success then
'''
endif
end Class
.... somewhere else
Public Shared Function CopyObject(of T)(Byval Source as T, Byref Target as T) as Integer
'use serialization to make a deep copy but Target is 'Me'
End Function
I've found several examples of deep copying objects such as
Public Function Clone() As MyClass
Dim ms As Stream = New MemoryStream()
Dim bf As BinaryFormatter = New BinaryFormatter()
bf.Serialize(ms, Me)
ms.Position = 0
Return CType(bf.Deserialize(ms), MyClass)
End Function... but this serializes Me so Me is the Source, but really I want to serialise the Object passed into Me and set all the members of Me through serialization so Me is effectively the Target. Is there a way of doing this generically or is the only way to clone an object into Me on a member-by-member basis.
Thanks
Simon
I'm just beginning to learn VB.Net so if this seems dumb, please forgive me.
I have 2 objects and I want to make a deep copy of one into the other. I effectively want to clone an object into Me. (I'm also looking to learn about serialisation).
Public Class MyClass
Public Function Clone(AnObject as MyClass) as Integer
'ideally!!! make deep copy of all members of AnObject into Me
If CopyObject(AnObject, Me) = Success then
'''
endif
end Class
.... somewhere else
Public Shared Function CopyObject(of T)(Byval Source as T, Byref Target as T) as Integer
'use serialization to make a deep copy but Target is 'Me'
End Function
I've found several examples of deep copying objects such as
Public Function Clone() As MyClass
Dim ms As Stream = New MemoryStream()
Dim bf As BinaryFormatter = New BinaryFormatter()
bf.Serialize(ms, Me)
ms.Position = 0
Return CType(bf.Deserialize(ms), MyClass)
End Function... but this serializes Me so Me is the Source, but really I want to serialise the Object passed into Me and set all the members of Me through serialization so Me is effectively the Target. Is there a way of doing this generically or is the only way to clone an object into Me on a member-by-member basis.
Thanks
Simon