Vb. NET ByVal and ByRef--byval with a shallow copy and a deep copy

Source: Internet
Author: User
Tags integer reference resource shallow copy
Beginner vb.net, sum up ByVal and ByRef

1 pass through the ByVal variables, VB. NET duplicates a new variable that is equal to the source value. And ByRef is the equivalent of a reference.
For example, when we learn C, we get the swap () function


Imports System
' Test that Can ' t Swap A and B
Class MyApp
Public Shared Sub Main ()
Dim A as Integer = 0
Dim B as Integer = 1
Console.WriteLine ("Source:a" & A & "B" & B)
Fakeswap (a,b) ' After this fakeswap (a,b), A still are 0 and B still is 1
Console.WriteLine ("After Fakeswap:a" & A & "B" & B)
Swap (a,b) ' After this swap (A,B), A are 1 and B is 0
Console.WriteLine ("After Swap:a" & A & "B" & B)
End Sub

' Fake Swap Function:fakeswap ()
Shared Sub Fakeswap (ByVal InA As Integer, ByVal InB As Integer)
Dim TMP as Integer
TMP = InA
InA = InB
InB = TMP
End Sub
' Real Swap Function:swap ()
Shared Sub Swap (ByRef InA As Integer, ByRef InB As Integer)
Dim TMP as Integer
TMP = InA
InA = InB
InB = TMP
End Sub
End Class




2 Note: If ByVal passes An instance of a custom class, only the reference to that instance is replicated, and the resource to which the reference is directed is not replicated. --equivalent to a shallow copy in C + +.


Imports System

An instance of class A mya as a parameter of the function testa (ByVal InA as A), the result should be
'--passed by value to a shallow copy, just a copy of the instance referencing--a Mya and Ina share a resource
Class MyApp
Public Shared Sub Main ()
Dim MyA as A
Console.WriteLine ("The Original Resource of MyA is:" & Mya.resource)
' Call Testa ()
Testa (MyA)
Console.WriteLine ("After called" The ByVal Fun, the resource of MyA is: "& Mya.resource)
End Sub
' Function testa () Mya by value in order to change INA resource, in fact, the modification is also MYA resource
Shared Sub Testa (ByVal InA as A)
Ina.resource = 1
End Sub
End Class

' Class A has resource resource (integer)
Class A
Public Resource as Integer = 0
End Class




3 If you want to implement the "by value" (deep copy) of an instance of a class (not a reference), you must overridde the Clone () method or have a copy constructor specifically?
Method One:
<serializable>_
Class ABC
Xxx
End Class

Then use MemoryStream and BinaryFormatter (Streamcontext to use the file type), which is definitely a deep copy. But how do you implement a "copy construct" in C + +?
Cond...



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.