Cloning of objects in VB. NET
Hou Yongfeng
In 3DMAX, after creating an object (parent object), you can select Clone in the Edit menu. The following three options are available: Copy (to generate a child object of the same appearance, operations on both objects do not affect each other. Instance (parent-child interaction, parent object operations also affect child objects, and child objects) also has a Reference (not considered for the moment ).
In VB. NET, this problem also occurs. Create an instance of a complex object (which may have many different data types). After performing a series of operations, you want one or more intermediate variables (another instance) to save its status, we usually assign values to variables one by one, sometimes writing a long code :) this is actually the above Copy problem. For Instance, it is very easy to create an Instance and point it to the following:
Dim objA As New CResume 'cresume is a custom class that records resume information.
Then perform initialization, for example:
ObjA. Name = "AAA"
ObjA. Address = "Shanghai, China"
......
Dim objB As New CResume 'create an instance
ObjB = objA
In this case, objB and objA are in the same State. It is worth noting that objB changes a Name, for example:
ObjB. Name = "BBB"
In fact, the Name of objA also changes to "BBB", and the reason is very simple. They point to the same memory space.
Next we will focus on Object Clone, that is, the objects after Copy do not affect each other. The key is how to use a simple method to solve our problem, memoryStream and Binformatter can be used together to easily clone objects.
MemoryStream, as I mentioned before, is a stream that supports the storage area as memory.
Binformatter, mainly serialized and deserialized objects in binary format.
Main ideas:
First use the Serialize method of Binformatter to store the object in the MemoryStream (the operation is the same as that of other streams), and then Deserialize deserialization to get a streaming data, convert to the type of the original object. Is it easy? Haha. Let's take a look at the Code:
Code of the CResume class:
Imports System. IO
Imports System. Runtime. Serialization. formatters
Public Class CResume
'Note that the Serializable () attribute is added before the class; otherwise, serialization cannot be performed.
Dim m_Name As String 'name
Dim m_Address As String 'address
Public Property Address () As String
Get
Return m_Address
End Get
Set (ByVal NewAddress As String)
M_Address = NewAddress
End Set
End Property
Public Property Name () As String
Get
Return m_Name
End Get
Set (ByVal NewName As String)
M_Name = NewName
End Set
End Property
Public Function Clone () As CResume
Dim BF As New Binary. Binaryformatter ()
Dim MS As New MemoryStream ()
BF. Serialize (MS, Me)
MS. Position = 0
Return (CType (BF. Deserialize (MS), CResume ))
End Function
End Class
In form, you can call
Dim Resume1 As New CResume ()
Dim Resume2 As New CResume ()
Resume1.Name = "AAA"
Resume1.Address = "Shanghai, China"
Resume2 = Resume1.Clone ()
Resume1.Name = "BBB"
Resume1.Address = "Beijing, China"
Debug. WriteLine (Resume1.Name) 'is still AAA
Debug. WriteLine (Resume1.Address)
Summary: It mainly calls the stream and serialization Technology of VB. NET, which saves the tedious read/write operations and simplifies the code for object cloning.