Home All Groups Group Topic Archive Search About

Newbie Q: Serialization and Me

Author
22 Jun 2006 9:02 AM
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

Author
23 Jun 2006 5:56 PM
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

Simon Woods wrote:
Show quoteHide quote
> 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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>Public Class MyClass</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Public Function Clone(AnObject
> as MyClass) as Integer</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 'ideally!!!
> make deep copy of all members of AnObject into Me</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; If
> CopyObject(AnObject, Me) = Success then</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp; '''</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> endif</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>end Class</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>... somewhere else</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Public Shared Function
> CopyObject(of T)(Byval Source as T, Byref Target as T) as Integer</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 'use
> serialization to make a deep copy but Target is 'Me'</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; End Function&nbsp;</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; </FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</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&nbsp;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>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>Simon</FONT></DIV></BODY></HTML>
>
> ------=_NextPart_000_0025_01C695E3.043C3FD0--
Author
24 Jun 2006 7:18 AM
Simon Woods
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?


Show quoteHide quote
> 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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>Public Class MyClass</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Public Function
>> Clone(AnObject
>> as MyClass) as Integer</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> 'ideally!!!
>> make deep copy of all members of AnObject into Me</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; If
>> CopyObject(AnObject, Me) = Success then</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> &nbsp;&nbsp;&nbsp; '''</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> endif</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>end Class</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>... somewhere else</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Public Shared
>> Function
>> CopyObject(of T)(Byval Source as T, Byref Target as T) as
>> Integer</FONT></DIV> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;
>> &nbsp;&nbsp;&nbsp; 'use
>> serialization to make a deep copy but Target is 'Me'</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; End
>> Function&nbsp;</FONT></DIV> <DIV><FONT face=Arial
>> size=2>&nbsp;&nbsp;&nbsp; </FONT></DIV> <DIV><FONT face=Arial
>> size=2></FONT>&nbsp;</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&nbsp;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>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>Simon</FONT></DIV></BODY></HTML>
>>
>> ------=_NextPart_000_0025_01C695E3.043C3FD0--
Author
26 Jun 2006 3:39 PM
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





Simon Woods wrote:
Show quoteHide quote
> 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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
> >> <DIV><FONT face=Arial size=2>Public Class MyClass</FONT></DIV>
> >> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> >> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Public Function
> >> Clone(AnObject
> >> as MyClass) as Integer</FONT></DIV>
> >> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> >> 'ideally!!!
> >> make deep copy of all members of AnObject into Me</FONT></DIV>
> >> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; If
> >> CopyObject(AnObject, Me) = Success then</FONT></DIV>
> >> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> >> &nbsp;&nbsp;&nbsp; '''</FONT></DIV>
> >> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> >> endif</FONT></DIV>
> >> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> >> <DIV><FONT face=Arial size=2>end Class</FONT></DIV>
> >> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> >> <DIV><FONT face=Arial size=2>... somewhere else</FONT></DIV>
> >> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Public Shared
> >> Function
> >> CopyObject(of T)(Byval Source as T, Byref Target as T) as
> >> Integer</FONT></DIV> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;
> >> &nbsp;&nbsp;&nbsp; 'use
> >> serialization to make a deep copy but Target is 'Me'</FONT></DIV>
> >> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; End
> >> Function&nbsp;</FONT></DIV> <DIV><FONT face=Arial
> >> size=2>&nbsp;&nbsp;&nbsp; </FONT></DIV> <DIV><FONT face=Arial
> >> size=2></FONT>&nbsp;</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&nbsp;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>&nbsp;</DIV>
> >> <DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
> >> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> >> <DIV><FONT face=Arial size=2>Simon</FONT></DIV></BODY></HTML>
> >>
> >> ------=_NextPart_000_0025_01C695E3.043C3FD0--
Author
27 Jun 2006 5:41 AM
Simon Woods
Thanks ... that IS something to consider

Simon

TDC wrote:
Show quoteHide quote
> 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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
>>>> <DIV><FONT face=Arial size=2>Public Class MyClass</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Public Function
>>>> Clone(AnObject
>>>> as MyClass) as Integer</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>>>> 'ideally!!!
>>>> make deep copy of all members of AnObject into Me</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>>>> If CopyObject(AnObject, Me) = Success then</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>>>> &nbsp;&nbsp;&nbsp; '''</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>>>> endif</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=Arial size=2>end Class</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=Arial size=2>... somewhere else</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Public Shared
>>>> Function
>>>> CopyObject(of T)(Byval Source as T, Byref Target as T) as
>>>> Integer</FONT></DIV> <DIV><FONT face=Arial
>>>> size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 'use
>>>> serialization to make a deep copy but Target is 'Me'</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; End
>>>> Function&nbsp;</FONT></DIV> <DIV><FONT face=Arial
>>>> size=2>&nbsp;&nbsp;&nbsp; </FONT></DIV> <DIV><FONT face=Arial
>>>> size=2></FONT>&nbsp;</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&nbsp;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>&nbsp;</DIV>
>>>> <DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
>>>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=Arial size=2>Simon</FONT></DIV></BODY></HTML>
>>>>
>>>> ------=_NextPart_000_0025_01C695E3.043C3FD0--