VBS (ASP) ByVal ByRef function Call usage instructions _vbs

Source: Internet
Author: User

1. ByVal value: A way to pass a parameter value instead of the address to a procedure, which makes the procedure accessible to a copy of the variable. As a result, the process cannot change the true value of the variable.
2. ByRef value: A way to pass a parameter address instead of a value to a procedure, which allows the procedure to access the actual variable. As a result, the process can change the true value of the variable. Unless otherwise stated, parameters are passed by address.
3, the system default is ByRef transfer value .

Example:

Copy Code code as follows:

<script language= "VbScript" >
Dim a
A=0
document.write "A=0"
document.write "<br/>sub Change (ByRef ar) <br/>"
Change a
Document.Write A
A=0
document.write "<br/>sub change2 (ByVal ar) <br/>"
Change2 A
Document.Write A
A=0
document.write "<br/>sub change3 (AR) <br/>"
Change3 A
Document.Write A
A=0
document.write "<br/>function change4 (ByRef ar) <br/>"
Change4 A
Document.Write A
A=0
document.write "<br/>function change5 (ByVal ar) <br/>"
Change5 A
Document.Write A
A=0
document.write "<br/>function change6 (AR) <br/>"
Change6 A
Document.Write A
A=0
Sub Change (ByRef ar)
ar=111
End Sub
Sub Change2 (ByVal ar)
ar=222
End Sub
Sub Change3 (AR)
ar=333
End Sub
function change4 (ByRef ar)
ar=444
End Function
function change5 (ByVal ar)
ar=555
End Function
function Change6 (AR)
ar=666
End Function
</SCRIPT>

=======================
Results:
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
Description VBS defaults to ByRef, this is the same as VB, by address.

Then give a small example, we run to see the effect!
Copy Code code as follows:

<%
Dim i,j,p,m
i = 10
j = 12
Response.Write i& "Hu Jintao" &j& "<br>"
Call Fun2 (I,J)
Response.Write i& "Hu Jintao" &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
%>

As you can see from the above example:
1, ByVal Transfer Value does not change the value of the global variable.
2. A ByRef value changes the value of a global variable.
3, the system default is ByRef transfer value.

As to when to use it? This depends on the actual situation of their own!

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


Description of VB6
Don't say anything, answer the question:
Copy Code code as follows:

Function Test (a)
A = a + 1
End Function

c = 1
'------------------------------------
Problem
' Say the results of each of the following four methods, please?
'------------------------------------
' Method One
Test C

' Method two
' Test (c)

' Method Three
' Test (c+1)

' Method Four
' Call Test (c)

MsgBox C

Correct answers: 2, 1, 1, 2, respectively
Did you answer all the answers? All right, you can return directly, not correct words continue to look down.

Traced back
The function parameters of almost all programming languages have a value and a reference. Our VB brother is no exception, and, she chose to pass the reference as its default way, she is not picky about all the variable types, all by default by the way of reference, including integer variable (int). This is also the most fundamental reason for the failure of the sword in Huashan.
You can use the ByVal (Pass Value) and ByRef (Reference) keyword to indicate how the parameter is passed before the parameter:

' Value-transfer method, C-value unchanged
Copy Code code as follows:

Function Test (ByVal a)
A = a + 1
End Function
c = 1
Test C
' C = 1

' Value-transfer method, C-value unchanged
Copy Code code as follows:

Function Test (ByRef a)
A = a + 1
End Function
c = 1
Test C
' C = 2

Understand
In general, there are two methods of VB6 function functions called:
Func Params
If method one: Test C

Call Func (Params)
If method four: Call Test (c)


Therefore, in the sword of method one and method four essentially the same, parameter C is passed through the default pass reference, after the completion of the function test, the value of C will change. So the result of method one and method four is: 2
However, there seems to be a popular way to invoke this method:


Func (Expression)
If method two: Test (c) and method three: Test (c+1)

Notice that the contents of the parentheses are not params, but expression, an expression, and the result of the expression is saved in a temporary variable to the function body, and the temporary variable is destroyed after the function call completes. Therefore, when you call method two o'clock, the expression (c) evaluates to 1 and is saved to a temporary variable passed into the function test, and the original C value does not change after the function completes. Similarly, the invocation of method three is more intuitive, and (c+1) expression operations are saved to a temporary variable passing in test, and the original C value does not send a change. So the result of method two and method three is: 1

Apprenticeship
Summarized as follows:
function parameters in VB6 and VBS are ByRef-type by default.

When the Test (c) method is called, VB will think that you pass is not a variable, but an expression: (c), the expression of the results, although the same as the C value, but is stored in a temporary variable, this temporary variable changes will not affect the original variable C

Related Article

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.