Byval transfers a copy of the parameter memory to the caller. That is to say, the stack pressure is directly the passed value.
The actual address of the byref transfer parameter memory is sent to the caller. That is to say, the stack medium pressure is the actual content address. The caller can directly change the content of the address.
Byval indicates that the transmitted value source data will not be modified.
You can use this value as your local variable.
Byref is the transfer address, and the source data may be modified.
Your operations on this variable will affect the variable you passed in, just like the pointer.
instance: copy Code the code is 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 button#click (sender as object, e as eventargs) handles button1.click
dim A as int32
A = 100
Add1 (a)
msgbox ("A value:" & A) 'display: a Value 100
Add2 (a)
msgbox ("A value:" & A) ': the value of A is 200, because the parameter no in Add2 is byref, that is,
'is passed by address, after no is modified in Add2, the value of
'Source parameter A is also modified.
end sub
--------------------------------------
3. byval and byref
The parameter value passed by byval, and the address of the parameter passed by byref. Here, we do not need to distinguish between passing pointer, passing address, and transferring reference. In VB, they are three different statements of one thing, even in VB documents, these terms are mixed (but pointers and references must be distinguished in C ++)
First contact with the aboveProgramSwapptr friends must find out where the byval is to be added in the copymemory call, and where the byval is not added (if the byval is not added, the default byref of VB is used)
An accurate understanding of the difference between passing a value and passing an address (pointer) is the basis for correct pointer usage in VB.
Now, let's look at this problem through a simple experiment. The procedure is as follows:
[Procedure 3]: Experience byval and byrefCopy codeThe Code is as follows: Sub testcopymemory ()
Dim K as long
K = 5
Note: copymemory byval varptr (K), 40000, 4
Debug. Print K
End sub
The purpose of the statement at the preceding note is to assign K to 40000, which is equivalent to statement K = 40000. You can test it in the "now" window, we will find that the value of K is indeed 40000.
In fact, the above statement is translated into vernacular:
-----------------------------------------------------------------
4 bytes are copied from the temporary variable storing constant 40000 to the memory where the variable k is located.
-----------------------------------------------------------------
Now let's change the statement at a note. If it is changed to the following statement:
Note2: copymemory byval varptr (K), byval 40000, 4
The meaning of this statement is: Copy 4 bytes from address 40000 to the memory where the variable k is located. Because we do not have permission to access the memory where address 40000 is located, the operating system will give us an access violation memory unauthorized access error, telling us "An error occurred while trying to read the memory at location 0x00009c40, the memory cannot be 'read '".
Let's change it to the following statement.
Note3: copymemory varptr (K), 40000, 4
The meaning of this statement is that four bytes are copied from the temporary variable that saves constant 40000 to the temporary variable that saves the memory address value of variable k. This will not cause an out-of-memory unauthorized access error, but the value of K has not changed.
We can change the program to clear the difference, as shown in the following procedure 4:
[Program 4]: 'Let's see where our stuff has been copied. Copy code The Code is as follows: Sub testcopymemory ()
Dim I as long, K as long
K = 5
I = varptr (k)
Note 4: 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
Because the default byref is used in note4, the address of I is passed (that is, the pointer to I), so constant 40000 is copied to variable I, so the value of I is 40000, the value of K remains unchanged. However, before note4: I = varptr (k), the intention is to use I as a pointer. In this case, we must use byval as note5 to pass the pointer I. Since I is a pointer to the variable k, the final constant 40000 is