Differences between ByVal and Byref

xiaoxiao2021-03-05  25

ByVal transmits a copy of the parameter memory to the caller. That is, it is directly transmitted in the stack.

BYREF transmits the actual address of the parameter memory to the caller. That is, it is pressed into the address of the actual content in the stack. The caller can directly change the content in this address.

BYVAL is the transfer value source data is not modified

You can use this value as your own local variable.

BYREF is a delivery address, and the source data may be modified.

Your operation of this variable will affect the variable you introduced, just like the feeling of the pointer.

Example:

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 ("The value of A:" & a) 'shows: a value of 100

Add2 (a)

MsgBox ("A value is:" & a) 'Display: A value is 200, because the parameter NO in Add2 is Byref, ie

'Press address, so after modifying NO in Add2, it will result

The value of 'source parameter A is also modified.

End Sub

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3, ByVal and Byref

BYVAL passed the parameter value, and the address of the parameter passed by ByRef. Here, we don't have to distinguish between password / biographical address / biography, in VB, they are all three different statements, even if there is also a VB document, there are also these terms (but in C indeed To distinguish between pointers and references)

In the first time you contact the program of the two Swapptr, you must figure it out in the copyMemory call in it, where to add byval, where you are not adding (not byval), use VB default byref)

The accurate understanding of the difference between the pass value and the address (pointer) is the basis of the correct use of the pointer in VB.

Now one of the simplest experiments to see this problem, as shown below:

[Procedure 3]: 'Experience Byval and Byref

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 Note, is to assign K to 40000, equivalent to the statement K = 40000, you can test it in the "Immediate" window, will find that K's value is indeed 40000.

In fact, the above statement is translated into a white dialect:

-------------------------------------------------- ---------------

That is to copy 4 bytes to the memory of the variable k from the timeout of the preservation constant 40000.

-------------------------------------------------- ---------------

Now let's change the statement at Note, if it is changed to the following statement:

Note2: CopyMemory Byval Varptr (k), Byval 40000, 4

The meaning of this sentence is, and copy 4 bytes from the address 40000 to the memory where the variable k is located. Due to the memory of the address 40000 we have no right to access, the operating system will give us an Access Viocation memory access error, tell us "Attempt to read the location 0x00009C40 error, the memory cannot be 'read'." We changed to the following statement to see.

Note3: CopyMemory Varptr (k), 40000, 4

The meaning of this sentence is, and copy 4 bytes from the provisional variable of the saving constant 40000 to the temporary variable of the memory address value of the saving variable k. This will not have memory access error, but the value of K has not changed.

We can change the program to a more clearly rescue this difference, as shown below:

[Procedure 4]: 'Look at our things to have been copied. Where is it?

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 Note4 uses the default Byref, the address (that is, point to i's pointer), so the constant 40000 is copied to the variable I, so the value of I is 40000, and the value of K has not changed. However, there is: i = varptr (k) before Note4, is intended to use I itself as a pointer to use. At this time, we must use BYVAL as Note5 to deliver the pointer I, since i is a pointer to the variable k, so the last constant 40000 is copied in the variable k.

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

New Post(0)