VBS语法:ByRef and ByVal Parameters
来源:互联网 发布:防水喷雾 知乎 编辑:程序博客网 时间:2024/05/20 03:47
ByRef and ByVal Parameters
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 = 5End Sub Dim MyArg MyArg = 123TestSub 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 = 5End Sub Dim MyArg MyArg = 123TestSub 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 = 5End Sub Dim MyArg MyArg = 123TestSub 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 = 5End SubDim MyArgMyArg = 123TestSub (MyArg)' MyArg is still 123.
A 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 = 5End FunctionDim MyArg, Result MyArg = 123TestFunction (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 = 5End SubDim MyArgMyArg = 123Call 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 = 6End FunctionDim MyArg1, MyArg2, ResultMyArg1 = 123MyArg2 = 124Result = 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 MyValueEnd ClassSub TestMethod (ByRef MyParam) MyParam = 5End SubDim custSet cust = New Customercust.MyValue = 123TestMethod cust.MyValue' cust.MyValue is still 123.
- VBS语法:ByRef and ByVal Parameters
- ByRef vs. ByVal
- ASP byval byref 区别
- Byval和ByRef
- ByVal和ByRef
- ByRef和ByVal
- ByVal与ByRef
- Byval与Byref的区别
- ByVal和ByRef的区别
- ByVal和ByRef的区别
- VB中ByVal和ByRef
- ByVal与ByRef的区别
- ByVal 与 ByRef的区别
- byref与byval的区别
- Byval 和 Byref的区别
- ByVal和ByRef有什么区别
- byval 和byref的区别,今天刚明白。
- 数据搬运工-VB中参数(ByVal,ByRef)的传递
- WinSock学习(1):Select模型
- 如何用dos运行cpp
- png背景图在IE6下无法用background-position定位?
- Linux驱动模块初始教程
- peoplesoft documents
- VBS语法:ByRef and ByVal Parameters
- 杭电 2854Central Meridian Number 完全不懂的数论
- sql 取最大的id 的内容
- 今早坐92路,做了50分钟才从富强街到桂平路
- 传感器概念
- ThinkPHP2.1培训资料
- 初识NoSql
- 关于传感器的技术参数
- android获取屏幕宽高方法