Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the main idea of VB.NET object cloning?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the main idea of VB.NET object cloning, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The operation of objects in VB.NET programming language is an important operation skill. For a programming language with object-oriented characteristics, if we want to thoroughly grasp the application of this language, we should grasp these characteristics well. Let's take a look at some of the basic concepts of VB.NET object cloning.

In 3DMAX, after making an object (parent object), you can choose Clone in the Edit menu. There are three options below: Copy (generate a child object with the same appearance, the operation of the two does not affect each other), Instance (father-son interaction, the operation of the parent object also affects the child object, and the child object is the same), and a Reference (not considered for the time being).

The same problem will be encountered in VB.NET. Create an instance of a complex object (there may be many different data types). After a series of operations, if you want an intermediate variable or several (other instances) to hold its state, we usually assign values one by one, sometimes writing a long code:) this is actually the Copy problem mentioned above. For Instance, it's simple to create a new instance and point to it:

Dim objA As New CResume 'Cresume

Is a custom class that records resume information

Then initialize it, such as:

ObjA.Name= "AAA" objA.Address= "Shanghai,China". Dim objB As New CResume 'create a new instance objB=objA

In this case, objB has the same state as objA, and it is worth noting that objB changes a Name, such as:

ObjB.Name= "BBB"

In fact, objA's Name has also become "BBB" for a simple reason. They point to the same memory space.

Sharing of specific operation skills of VB.NET structure variables

VB.NET sharing skills for creating new variables

A brief discussion on the common creation methods of VB.NET fixed-value variables

A brief discussion on the correct realization method of creating Array by VB.NET

Sharing skills for initializing array variables in VB.NET

Next we focus on the problem of Clone of objects, 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 together can easily achieve VB.NET object cloning.

MemoryStream, as I mentioned before, supports streams that store for memory.

BinFormatter, which mainly serializes and deserializes objects in binary form.

Main ideas:

First use BinFormatter's Serialize method to store the object in the MemoryStream stream (the operation is the same as other Stream), and then Deserialize deserialize to get a streaming data, which can be converted to the original object type. Isn't it easy? He he. Take a look at the code and say:

The code for the CResume class:

Imports System.IO Imports System.Runtime.Serialization.Formatters

< Serializable()>

Public Class CResume 'notice that the class is preceded by the Serializable () attribute Otherwise, 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) cannot be serialized 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 = "office:smarttags" / > Shanghai,China "Resume2 = Resume1.Clone () Resume1.Name =" BBB "Resume1.Address =" Beijing,China "Debug.WriteLine (Resume1.Name)'is still AAA Debug.WriteLine (Resume1.Address)

Summary: the streaming and serialization technology of VB.NET is mainly invoked, which eliminates the tedious read and write operations of VB.NET object cloning and simplifies the code.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report