Re: Newbie Q: Serialization and Me
- From: "Simon Woods" <simon.woodsUseless@xxxxxxxxxx>
- Date: Tue, 27 Jun 2006 06:41:50 +0100
Thanks ... that IS something to consider
Simon
TDC wrote:
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
Simon Woods wrote:
TDC wrote:
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?
Simon Woods wrote:
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
------=_NextPart_000_0025_01C695E3.043C3FD0
Content-Type: text/html; charset=Windows-1252
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 3311
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html;
charset=windows-1252"> <META content="MSHTML 6.00.2900.2912"
name=GENERATOR> <STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>Hi</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I'm just beginning to learn VB.Net so
if this seems
dumb, please forgive me. </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>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). </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Public Class MyClass</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> Public Function
Clone(AnObject
as MyClass) as Integer</FONT></DIV>
<DIV><FONT face=Arial size=2>
'ideally!!!
make deep copy of all members of AnObject into Me</FONT></DIV>
<DIV><FONT face=Arial size=2>
If CopyObject(AnObject, Me) = Success then</FONT></DIV>
<DIV><FONT face=Arial size=2>
'''</FONT></DIV>
<DIV><FONT face=Arial size=2>
endif</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>end Class</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>... somewhere else</FONT></DIV>
<DIV><FONT face=Arial size=2> Public Shared
Function
CopyObject(of T)(Byval Source as T, Byref Target as T) as
Integer</FONT></DIV> <DIV><FONT face=Arial
size=2> 'use
serialization to make a deep copy but Target is 'Me'</FONT></DIV>
<DIV><FONT face=Arial size=2> End
Function </FONT></DIV> <DIV><FONT face=Arial
size=2> </FONT></DIV> <DIV><FONT face=Arial
size=2></FONT> </DIV> <DIV><FONT face=Arial size=2>I've found
several examples of deep copying objects
such as</FONT></DIV>
<DIV><PRE class=Code><FONT face=Arial size=2>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</FONT></PRE></DIV>
<DIV><FONT face=Arial size=2>... 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.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Simon</FONT></DIV></BODY></HTML>
------=_NextPart_000_0025_01C695E3.043C3FD0--
.
- References:
- Newbie Q: Serialization and Me
- From: Simon Woods
- Re: Newbie Q: Serialization and Me
- From: TDC
- Re: Newbie Q: Serialization and Me
- From: Simon Woods
- Re: Newbie Q: Serialization and Me
- From: TDC
- Newbie Q: Serialization and Me
- Prev by Date: How to have .net code call pager or phone
- Next by Date: Re: Difference between Class and module
- Previous by thread: Re: Newbie Q: Serialization and Me
- Next by thread: Broken property panel
- Index(es):
Relevant Pages
|