Re: Newbie Q: Serialization and Me

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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>&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--


.



Relevant Pages

  • Re: Newbie Q: Serialization and Me
    ... Dim bf As BinaryFormatter = New BinaryFormatter ... I have 2 objects and I want to make a deep copy of one into the ... Target as T) as Integer 'use serialization to make a deep ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How deep should a deep copy go? / Is an object that contains value type still a reference type?
    ... To implement deep copy you can use serialization, ... In some cases you only need to do a partial clone as far as the whole graph is concerned (ie. clone an order attached to a customer, but let it refer to the same customer as the original order). ... At that point you will have some problems using serialization, which tends to do a complete graph serialization, containing the customer and whatnot. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Serialization, XSD to class, and deep copying?
    ... I said road to nowhere based on what I found on the net regarding deep ... implement ICloneable on the derived class ... our Clone() method uses serialization & deserialization create new ... might be in terms of performance, in that we're using serialization ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Backing Up Objects
    ... types of Objects where clone() does an actual clone and create a new ... using serialization. ... transient field with a particular name and there no longer is or a ... readResolve method that returns the singleton instance from a static ...
    (comp.lang.java.programmer)
  • Re: Why cant I copy a collection class from one instance to another?
    ... it is common to true object programming environments. ... still write a clone method, but the properties and methods of the object ... The side benefit of this serialization process is that the code to clone ...
    (comp.databases.ms-access)