Saturday, November 5, 2011

VBScript Passing Values ByRef or ByVal

VBScript Passing Values ByRef or ByVal

Below are few simple but quite important differences between passing values by ByRef or ByVal.

ByRefByVal
Function fun_1( ByRef var_1)
var_1= var_1+ 1
End Function

Dim x: x = 3
'Pass parameter to the function ByRef
fun_1 x
MsgBox x

Result is 4
Function fun_1( ByVal var_1)
var_1= var_1+ 1
End Function

Dim x: x = 3
'Pass parameter to the function ByVal
fun_1 x
MsgBox x

Result is 3
Another way to pass parameter ByRef
Function fun_1( var_1)
var_1= var_1+ 1
End Function

Dim x: x = 3
'Pass parameter to the function ByRef
call fun_1 (x)
MsgBox x

Result is 4
Another way to pass parameter ByVal
Function fun_1( var_1)
var_1= var_1+ 1
End Function

Dim x: x = 3
'Pass parameter to the function ByVal
call fun_1 ((x))
MsgBox x

Result is 3
Function abc(a)
if Not IsArray(a) Then Exit Function
a(0) = "Text"
End Function

Dim w(2), x
For x = 0 To 2
W(x)= x
Next

call abc(w)
for i=0 to UBOUND (w)
Wscript.Echo w(i)
Next

Shows Text,1 and 2
Function abc(a)
if Not IsArray(a) Then Exit Function
a(0) = "Text"
End Function

Dim w(2), x
For x = 0 To 2
W(x)= x
Next

call abc((w))
for i=0 to UBOUND (w)
Wscript.Echo w(i)
Next

Shows 0,1 and 2

By default, arguments are passed by reference.

VBScript chooses the argument association method as follows:
-If the argument is a simple variable, array, or array element, VBScript uses the "by reference" method.
-If the argument is an expression, VBScript uses the "by value" method.

The method to use can also be specified explicitly when the procedure is declared. To specify the argument association method, prefix procedure parameter with the ByVal or ByRef keywords.

The presence of the ByVal or ByRef keywords modifies the way VBScript chooses the argument association method as follows:
- If the parameter corresponding to an argument specifies ByVal, VBScript passes the argument by value.
- If the argument is an expression, VBScript passes the argument by value even if the ByRef keyword is specified in the corresponding parameter.
- If the argument is a variable, array, or array element and the corresponding parameter does not specify ByVal, VBScript passes the argument by reference. [Via: Windows Script Host By Tim Hill]

Below 3 examples will help you explore more on passing arguments by ByRef and ByVal:

Example 1 - passing arguments by ByRef and ByVal

Sub PassArgs(ByRef x, ByVal y, z)
x = 10
y = 11
z = 12
End Sub

Dim a, b, c
PassArgs a, b, c

Msgbox a ‘shows 10
Msgbox b ‘shows empty messagebox
Msgbox c ‘shows 12

Here 3 global variables are used as arguments. The first parameter is declared ByRef, and so the variable becomes 10 when the assignment statement x= 10 executes. The second parameter is declared ByVal, and so the varibale b is unaltered by the PassArgs procedure. The third parameter uses default parameter passing. Because the argument is a varibale, the argument is passed by reference, and the assignment statement z= 12 does alter the value of the variable c.

Example 2 - passing arguments by ByRef and ByVal

Sub PassArgs(ByRef x, ByVal y, z)
x = 10
y = 11
z = 12
End Sub

PassArgs 1, 2, 3

Here three expressions (actually just numbers) are used as arguments. Using expressions as arguments forces VBScript to pass the arguments by value regardless of the procedure declaration.

Example 3 - passing arguments by ByRef and ByVal

Sub PassArgs(ByRef x, ByVal y, z)
x = 10
y = 11
z = 12
End Sub

Dim a, b, c
PassArgs (a), (b), (c)

Msgbox a ‘shows empty messagebox
Msgbox b ‘shows empty messagebox
Msgbox c ‘shows empty messagebox

Here it might appear that three global variables are again passed as arguments. However, enclosing a variable in parentheses makes VBScript treat the variable as an expression. Therefore, in this case, the arguments are again passed by value, although the arguments are variables. Enclosing a variable name in parentheses like this will always ensure that the argument is passed by value, regardless of the procedure declaration. This ensures that the variable value is never altered by the procedure. [via: Windows Script Host By Tim Hill]