ByRef and ByVal in VBScript

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.

Leave a Comment