Cloning of objects in VB.NET

zhaozj2021-02-17  73

Cloning of objects in VB.NET

Hou Yongfeng

Inside 3DMAX, after a good object (parent), you can select Clone in the EDIT menu. There are three options below: Copy (generated a child of the same appearance, the operation does not affect each other), instance (father and child interact The operation of the parent also affects the child, the child is also the same as the child), and there is a Reference (not considered).

In VB.NET, you will also encounter such problems. Create an instance of a complex object (there may be many different data types), after a series of operations, want a middle variable or several (additional instances) to save its status, we generally take a variable value assignment, When you write a long code :) This is actually the above copy problem. For instance, it is very simple, create a new instance, pointing to it:

DIM OBJA AS New CRESUME 'CRESUME is a customized record resume information class

Then initialize, for example:

Obja.name = "aaa"

Obja.address = "Shanghai, China"

......

Dim objb as new cresume 'New an instance

Objb = Obja

In this case, Objb has the same state as Obja, it is worth noting that OBJB changes a Name, such as:

Objb.name = "bbb"

In fact, Obja's NAME has also become "BBB", and the reason is very simple. They point to the same memory space.

Below we focus on discussing the Clone problem of the object, that is, each object after COPY does not affect each other. How is the key to solve our problems, MemoryStream and Binformatter can simply clone objects.

MemoryStream, I have previously mentioned that the stream supporting the storage area is memory.

Binformatter, mainly serialized and reverse sequenced in binary form.

Main ideas:

First use binformatter's Serialize method, store objects in the MemoryStream stream (the same as other stream), then Deserialize deceased, get a streaming data, converted to the original object. Is not it simple? Ha ha. Look at the code and say:

CRESUME class code:

Imports system.io

Imports system.Runtime.Serialization.Formatters

public class cresume

'Attention to the front plus the serializable () attribute, otherwise you can not serialize

DIM M_NAME AS STRING 'Name

DIM M_ADDRESS AS STRING '

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_NameEnd 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.DSERIALIZE (MS), CRESUME)

END FUNCTION

END CLASS

Calling in Form

Dim Resume1 As New Creesume ()

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: Mainly call the stream and serialization technique of VB.NET, so that the clone of the object is removed from cumbersome read and write operations, but also simplifies the code.

转载请注明原文地址:https://www.9cbs.com/read-30881.html

New Post(0)