VBS语法:ByRef and ByVal Parameters

ByRef and ByVal Parameters

Windows Scripting 5.8

Updated: September 2009

When you call a subroutine or function, each argument can be passed by reference or by value. When an argument is passed by reference, the called procedure can change the value of the variable. The change persists after the procedure is called. When an argument is passed by value, any changes that the called procedure makes to the value of the variable do not persist after the procedure is called.

In a Sub or Function declaration, each parameter can be specified as ByRef or ByVal. If neither is specified, the default is ByRef.

If ByVal is specified, the corresponding argument is always passed by value when the subroutine is called.

If ByRef (or neither) is specified, the argument can be passed by reference or by value when the subroutine is called. The argument is passed by value if it is enclosed in parentheses, and if the parentheses do not apply to the parameter list. The argument is also passed by value if the variable sent as an argument is in a class. Otherwise, it is passed by reference.

The following table summarizes this.

Keyword specified

Argument is passed by

ByVal

Value.

ByRef (default)

Reference or value, depending on other factors.

In the following example, the ByRef keyword is specified for the MyParam variable. When the subroutine is called, a reference is passed to the variable MyArg. When the subroutine changes the value of MyParam, this also changes the value of MyArg. The local variable MyParam becomes an alias for MyArg.

Sub TestSub(ByRef MyParam) 
    MyParam = 5
End Sub 

Dim MyArg 
MyArg = 123

TestSub MyArg
' MyArg in changed in TestSub to 5.

In the following example, the ByVal keyword is used. Therefore, the argument is passed by value. The subroutine changes the value of MyParam. However, the value of MyArg remains unchanged.

Sub TestSub(ByVal MyParam) 
    MyParam = 5
End Sub 

Dim MyArg 
MyArg = 123
TestSub MyArg
' MyArg is still 123.

The default is ByRef if ByRef or ByVal is not specified. In this example, ByRef applies to the MyParam variable because neither ByRef nor ByVal is specified.

Sub TestSub(MyParam)
    MyParam = 5
End Sub 

Dim MyArg 
MyArg = 123
TestSub MyArg
' MyArg is changed in TestSub to 5.

If the parameter is specified as ByRef, the argument can still be passed by value. This occurs if one of the following is true:

  • The argument is in parentheses, and the parentheses do not apply to the argument list.

  • The variable sent as an argument is in a class.

  • The argument being passed is not a variable, for example, 12.

Argument in Parentheses

If the parameter is specified as ByRef, the argument can be passed by value if it is enclosed in parentheses. This is shown in the following example.

Sub TestSub(ByRef MyParam) 
    MyParam = 5
End Sub

Dim MyArg
MyArg = 123
TestSub (MyArg)
' MyArg is still 123.

ByRef parameter is passed by value if the argument is enclosed in parentheses and the parentheses do not apply to the argument list.

The parentheses apply to the argument list if one of the following is true:

  • The statement is a function call that has an assignment to the returned value.

  • The statement uses the Call keyword. (The Call keyword can optionally be used for a subroutine call, or for a function call without an assignment.)

In the following example, the second call to TestFunction has an assignment to the returned value. For that call, the parentheses enclosing the argument list do not cause the ByRef parameter to be passed by value.

Function TestFunction(ByRef MyParam) 
    TestFunction = MyParam * 2
    MyParam = 5
End Function

Dim MyArg, Result 
MyArg = 123

TestFunction (MyArg)
' MyArg is still 123.

Result = TestFunction (MyArg)
' MyArg is changed in TestSub to 5.

In the following example, a Call keyword is used. The parentheses that enclose the argument list do not cause the ByRef parameter to be passed by value.

Sub TestSub(ByRef MyParam) 
    MyParam = 5
End Sub

Dim MyArg
MyArg = 123

Call TestSub ((MyArg))
' MyArg is still 123.

Call TestSub (MyArg)
' MyArg is changed in TestSub to 5.

In the following example, the function has two ByRef parameters. In the function call, one argument is passed by value and the other argument is passed by reference.

Function TestFunction(MyParam1, MyParam2) 
    ' Both parameters are ByRef, because neither ByRef nor ByVal
    ' is specified for either parameter.
    TestFunction = MyParam1 * MyParam2
    MyParam1 = 5
    MyParam2 = 6
End Function

Dim MyArg1, MyArg2, Result
MyArg1 = 123
MyArg2 = 124

Result = TestFunction ((MyArg1), MyArg2)
' MyArg1 is still 123.
' MyArg2 is changed in TestFunction to 6.

Argument in a Class

If the parameter is specified as ByRef, the argument is passed by value if the variable sent as an argument is in a class.

In the following example, the MyParam parameter is specified as ByRef. The method is called by using the cust.MyValue argument. Because cust.MyValue is inside a class, the argument is passed by value.

Class Customer
    Public MyValue
End Class

Sub TestMethod (ByRef MyParam)
    MyParam = 5
End Sub

Dim cust
Set cust = New Customer
cust.MyValue = 123

TestMethod cust.MyValue
' cust.MyValue is still 123.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值