A copy of the ByVal transfer parameter memory to the callee. In other words, the stack is pushed into the direct is the value of the transmission.
BYREF transmits the actual address of the parameter memory to the callee. In other words, the stack is pressed into the actual content of the address. The callee can change the contents of the address directly.
ByVal is the Transfer value source data will not be modified
You can use this value as your own local variable.
ByRef is a delivery address, the source data may be modified
Your manipulation of this variable will have an effect on the variable you pass in, just like the sense of the pointer
Instance:
Copy Code code as follows:
Sub Add1 (ByVal No as Int32)
no=no+100
End Sub
Sub Add2 (ByRef No as Int32)
no=no+100
End Sub
Private Sub Button1_Click (sender as object,e as EventArgs) Handles Button1.Click
Dim A As Int32
a=100
ADD1 (a)
MsgBox ("A" value is: "& A)" shows: A has a value of 100
ADD2 (a)
The value of MsgBox ("a": "& A") shows: A has a value of 200 because the parameter no in ADD2 is ByRef, that is,
' is delivered by address, so modifying the No in ADD2 will cause
The value of the ' source parameter a ' is also modified.
End Sub
——————————————————————————————————————
3, ByVal and ByRef
ByVal the value of the parameter passed by ByRef, and the address of the parameter that is passed. Here, we do not have to distinguish between the transfer of pointers/address/reference to the different, in VB, they are basically a thing of three different versions, even if the VB document also has a place in the mix of these terms (but in C + + does have to distinguish between pointers and references)
First Contact above the program two swapptr friend, must be clear in the inside of the CopyMemory call, where to add ByVal, where Not Add (without ByVal is using VB default ByRef)
The exact understanding of the difference between the transfer value and the address (pointer) is the basis of using the pointer correctly in VB.
Now one of the simplest experiments to look at this problem, as the following program three:
"Program Three": Experience ByVal and ByRef
Copy Code code as follows:
Sub Testcopymemory ()
Dim K as Long
K = 5
Note:copymemory ByVal VarPtr (k), 40000, 4
Debug.Print K
End Sub
The above label note at the end of the statement is the purpose of the K assignment is 40000, equivalent to the statement k=40000, you can be in the "immediate" window test, you will find that the value of K is indeed 40000.
In fact, the above statement translates into vernacular:
-----------------------------------------------------------------
is to copy 4 bytes from the temporary variable that holds the constant 40000 to the memory in which the variable K resides.
-----------------------------------------------------------------
Now let's change the statement at a note, and change it to the following statement:
Note2:copymemory ByVal VarPtr (k), ByVal 40000, 4
The meaning of this sentence, from address 40000 copy 4 bytes to the memory of the variable K. Since address 40000 resides in the memory we do not have access to, the operating system will give us an access violation memory access error, tells us "an attempt to read the location 0X00009C40 memory error, the memory can not be ' read '."
Let's look at the following statement.
Note3:copymemory VarPtr (k), 40000, 4
In this sentence, copy 4 bytes from the temporary variable that holds the constant 40000 to the temporary variable where the value of the memory address in the save variable K resides. This does not give the memory an unauthorized access error, but the value of K does not change.
We can change the procedure to a clearer difference, as in the following procedure four:
"Program Four": ' See where our stuff has been copied.
Copy Code code as follows:
Sub Testcopymemory ()
Dim I as long, K as Long
K = 5
i = VarPtr (k)
Note4:copymemory I, 40000, 4
Debug.Print K
Debug.Print I
i = VarPtr (k)
Note5:copymemory ByVal I, 40000, 4
Debug.Print K
End Sub
Program output:
5
40000
40000
Since the NOTE4 place uses the default byref and passes the address of I (that is, the pointer to i), the constant 40000 is copied to the variable i, so the value of I is 40000, and the value of K is unchanged. However, before NOTE4 there are: I=varptr (k), the intention is to make I itself as a pointer to use. At this point, we must pass the pointer I with ByVal as NOTE5, and since I is a pointer to the variable k, the last constant 40000 is