Newbie Q: Serialization and Me

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
 
T

TDC

This is generally accomplished with a "copy constructor" as opposed to
cloning. At the very least, I'd rename the method you are trying to
create to "Copy" or "CopyFrom".

Why don't you just do a member-by-member assignment in the method
(either the copy constructor or the Copy methods), cloning or otherwise
manually creating and populating sub objects (since you said you wanted
a deep copy)?

Tom
 
S

Simon Woods

TDC said:
This is generally accomplished with a "copy constructor" as opposed to
cloning. At the very least, I'd rename the method you are trying to
create to "Copy" or "CopyFrom".

Why don't you just do a member-by-member assignment in the method
(either the copy constructor or the Copy methods), cloning or
otherwise manually creating and populating sub objects (since you
said you wanted a deep copy)?

Tom

Thanks Tom. I'll look into "copy constructors". I was wondering if there was
a way round doing it manually simply to make it more generic. I'm not
familiar with Reflection but perhaps that could help me, as well?
 
T

TDC

I'm sure you could come up with something using Reflection, but you may
lose a bit of control over the process if you want it completely
automatic. Probably no big deal though, I guess.

Another approach you could try is to place all of you data in a private
/ inner class and then use your serialization trick on *that* class, an
instance of which be obtained via friend / private / or superclass
methods. Once you have the clone of the data, just assign it to your
own data variable.

Note in this sample that the CloneData method is PRIVATE:



Public Class MyExampleClass

Private m_Data As New MyExampleClassData

Public Property Name() As String
Get
Return m_Data.Name
End Get
Set(ByVal Value As String)
m_Data.Name = Value
End Set
End Property

Public Sub CopyFrom(ByVal mec As MyExampleClass)
m_Data = mec.CloneData()
End Sub

Private Function CloneData() As MyExampleClassData

Dim ms As Stream = New MemoryStream
Dim bf As BinaryFormatter = New BinaryFormatter
bf.Serialize(ms, m_Data)
ms.Position = 0
Return CType(bf.Deserialize(ms), MyExampleClassData)

End Function

<Serializable()> _
Private Class MyExampleClassData
Public Name As String
End Class

End Class
 
S

Simon Woods

Thanks ... that IS something to consider

Simon
I'm sure you could come up with something using Reflection, but you
may lose a bit of control over the process if you want it completely
automatic. Probably no big deal though, I guess.

Another approach you could try is to place all of you data in a
private / inner class and then use your serialization trick on *that*
class, an instance of which be obtained via friend / private / or
superclass methods. Once you have the clone of the data, just assign
it to your own data variable.

Note in this sample that the CloneData method is PRIVATE:



Public Class MyExampleClass

Private m_Data As New MyExampleClassData

Public Property Name() As String
Get
Return m_Data.Name
End Get
Set(ByVal Value As String)
m_Data.Name = Value
End Set
End Property

Public Sub CopyFrom(ByVal mec As MyExampleClass)
m_Data = mec.CloneData()
End Sub

Private Function CloneData() As MyExampleClassData

Dim ms As Stream = New MemoryStream
Dim bf As BinaryFormatter = New BinaryFormatter
bf.Serialize(ms, m_Data)
ms.Position = 0
Return CType(bf.Deserialize(ms), MyExampleClassData)

End Function

<Serializable()> _
Private Class MyExampleClassData
Public Name As String
End Class

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top