Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ByRef and ByVal in VBScript

Tags:

vbscript

I'm facing something weird in VBScript. When writing a procedure where I want the parameter to be passed by reference, the way of calling this procedure changes the way the parameter is passed !

Here is an example :

Sub IncrementByRef(ByRef Value)    Value = Value + 1 End Sub  Sub IncrementByVal(ByVal Value)    Value = Value + 1 End Sub  Dim Num Num = 10 WScript.Echo "Num : " & Num IncrementByRef(Num) : WScript.Echo "IncrementByRef(Num) : " & Num IncrementByRef Num  : WScript.Echo "IncrementByRef Num : " & Num IncrementByVal(Num) : WScript.Echo "IncrementByVal(Num) : " & Num IncrementByVal Num  : WScript.Echo "IncrementByVal Num : " & Num 

And here is the output :

U:\>cscript //nologo byrefbyval.vbs Num : 10 IncrementByRef(Num) : 10 IncrementByRef Num : 11 IncrementByVal(Num) : 11 IncrementByVal Num : 11  U:\> 

When specifying the the parameters are passed ByVal, it works as expected, no matter the way the procedure is called. But when specifying the parameters are passed ByRef, it will work as expected if calling the procedure this way :

IncrementByRef Num 

but not this way :

IncrementByRef(Num) 

This seems weird to me. Is there a way to make sure the parameters are passed ByRef, no matter how the procedure is called ?

like image 907
Jérôme Avatar asked Oct 08 '09 13:10

Jérôme


People also ask

What is the difference between ByVal and ByRef?

ByRef = You give your friend your term paper (the original) he marks it up and can return it to you. ByVal = You give him a copy of the term paper and he give you back his changes but you have to put them back in your original yourself.

Is ByRef faster than ByVal?

With a large Byte() array, ByVal is faster than ByRef , but it's also negligible.

What is call by value and call by reference in VBScript?

In Call by value, a copy of the variable is passed whereas in Call by reference, a variable itself is passed. In Call by value, actual and formal arguments will be created in different memory locations whereas in Call by reference, actual and formal arguments will be created in the same memory location.

What is the difference between functions and sub procedures in VBScript explain using suitable examples?

There are two types of VBS procedures: Sub procedures and Function procedures. If you want to execute a series of statements without returning any value, then you can use sub procedures. If you want to execute a series of statements and return a value, then you need to use function procedures.


2 Answers

Eric Lippert has a great article on using parentheses in VBScript: What do you mean "cannot use parentheses?" Your example illustrates one of the points he mentions, namely: enclosing a ByRef argument in parentheses passes it as ByVal.

In short, parentheses in VBScript subroutine calls can be put not only around the argument list, but also around individual arguments (in which case they are forced ByVal). And VBScript only expects the argument list be enclosed in parentheses if the Call keyword is used. Since the IncrementByRef(Num) call doesn't use the Call keyword, VBScript treats parentheses as applied to the subroutine argument and thus passes it ByVal instead of ByRef.

Confusing, but that's the way it works.

like image 128
Helen Avatar answered Sep 28 '22 10:09

Helen


It's a feature, not a bug:
http://msdn.microsoft.com/en-us/library/ee478101.aspx

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.)

So try using the Call keyword or having it return a value.

like image 23
Joel Coehoorn Avatar answered Sep 28 '22 10:09

Joel Coehoorn