1. byval value transfer: A method of passing the parameter value instead of the address to the process. This allows the process to access the copy of the variable. As a result, the true positive value of the variable cannot be changed during the process.
2. byref value passing: a parameter address instead of passing the value to the process. This allows the process to access the actual variables. As a result, the real positive value of the variable can be changed in the process. Parameters are transmitted by address unless otherwise specified.
3,System DefaultYesByrefValue.
Example:
CopyCode The Code is as follows: <script language = "VBScript">
Dim
A = 0
Document. Write "A = 0"
Document. Write "<br/> sub change (byref AR) <br/>"
Change
Document. Write
A = 0
Document. Write "<br/> sub Change2 (byval AR) <br/>"
Change2
Document. Write
A = 0
Document. Write "<br/> sub Change3 (AR) <br/>"
Change3
Document. Write
A = 0
Document. Write "<br/> function change4 (byref AR) <br/>"
Change4
Document. Write
A = 0
Document. Write "<br/> function Change5 (byval AR) <br/>"
Change5
Document. Write
A = 0
Document. Write "<br/> function Change6 (AR) <br/>"
Change6
Document. Write
A = 0
Sub change (byref AR)
AR = 1, 111
End sub
Sub Change2 (byval AR)
AR = 1, 222
End sub
Sub Change3 (AR)
AR = 1, 333
End sub
Function change4 (byref AR)
AR = 1, 444
End Function
Function Change5 (byval AR)
AR = 1, 555
End Function
Function Change6 (AR)
AR = 1, 666
End Function
</SCRIPT>
======================================
Result:
A = 0
Sub change (byref AR)
111
Sub Change2 (byval AR)
0
Sub Change3 (AR)
333
Function change4 (byref AR)
444
Function Change5 (byval AR)
0
Function Change6 (AR)
666
This indicates that vbs is byref by default, which is the same as VB and is based on the address.
Here is a small example to show how it works!Copy codeThe Code is as follows: <%
Dim I, j, P, m
I = 10
J = 12
Response. Write I & "*******" & J & "<br>"
Call fun2 (I, j)
Response. Write I & "*******" & J & "<br>"
I = 10
J = 12
Call fun (I, j)
Response. Write I & "********" & J & "<br>"
Function fun2 (A, B)
A = 5
B = 6
Fun2 = 0
End Function
Function fun (byval A, byref B)
A = 5
B = 6
Fun = 0
End Function
%>
Through the above example, you can find that:
1. The value passed by byval does not change the value of the global variable.
2. The value passed by byref changes the value of the global variable.
3. The default value is byref.
When should I use it? This depends on your actual situation!
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.
VB6 description
Don't talk about anything. answer the following question:Copy codeThe Code is as follows: function test ()
A = a + 1
End Function
C = 1
'------------------------------------
'Problem:
'Are you sure you want to call the following four methods?
'------------------------------------
'Method 1
Test C
'Method 2
'Test (c)
'Method 3
'Test (C + 1)
'Method 4
'Call test (c)
Msgbox C
Correct answer: 2, 1, 1, 2
Are you all right? If you get all the correct answers, you can return them directly. If you don't get the correct answers, continue to look down.
Tracing
Almost allProgramming LanguageFunction parameters can be divided into values and references. Our VB dude is no exception. In addition, she chose to pass references as its default method. What's more, she is not picky about all variable types. All are referenced by default, including integer variables (INT ). This is also the most fundamental reason for the defeat of Huashan.
You can use the byval (pass value) and byref (pass reference) KEYWORDS before the parameter to specify the method for passing the parameter:
'Value transfer method, C value unchangedCopy codeThe Code is as follows: function test (byval)
A = a + 1
End Function
C = 1
Test C
'C = 1
'Value transfer method, C value unchangedCopy codeThe Code is as follows: function test (byref)
A = a + 1
End Function
C = 1
Test C
'C = 2
Comprehension
Generally, there are two function calling methods for VB6:
Func Params
For example, Method 1: Test C
Call func (Params)
Example 4: Call test (c)
Therefore, method 1 and method 4 in the sword are essentially the same. Parameter C is passed through the default reference transfer method. After the test function is executed, the value of C changes accordingly. Therefore, the result of method 1 and method 4 is: 2.
However, it seems that such a call method is still circulating in the rivers and lakes:
Func (expression)
For example, Method 2: Test (C) and method 3: Test (C + 1)
Note: No. The content in the brackets is not Params, but expression. It is an expression, and the calculation result of the expression is saved to a temporary variable to pass in the function body, after the function is called, the temporary variables are also destroyed. Therefore, when method 2 is called, the calculation result of expression (c) is 1 and saved to a temporary variable passed into function test. After the function is executed, the original C value does not change. Similarly, the call to method 3 is more intuitive. The (C + 1) expression result is saved to a temporary variable and passed to test. The original C value is not changed. Therefore, the result of method 2 and method 3 is: 1
Departure
Summary:
Function Parameters in VB6 and vbs are of the byref type by default.
When calling the test (c) method, VB considers that you are not passing a variable, but an expression: (c). This expression calculates the result, although it is the same as the value of C, but it is stored in a temporary variable. The change of this temporary variable does not affect the original variable C.