One thing I never knew: Parenthesis matter in QTP (and VBScript). And they surely make a difference. I discovered this during debugging a function call. There was different behaviour between these two function calls:
foo(bar)
call foo(bar)
After some research over the internet and in QTP self, I came to the following conclusions: Passing an argument to a function surrounded by parenthesis means: "Protect me" or in other words: treat me as byVal even if it is defined in the function as byRef.
Example:
sub samplesub (a, b) ' a and b are by default handled as byRef
a = a + b
end sub
And this is happening when we call samplesub:
x = 1
y = 2
z = 4
samplesub x, y
samplesub (z), y
msgbox x ' displays "3"
msgbox z ' displays "4" because z was passed as if it was passed byVal
The same applies when you call a function:
function samplefunc(c)
c = c^2-1
samplefunc = (c mod 2 = 1)
end function
q = 8
samplefunc q
msgbox q ' returns 63
' When you accidentally forgot to call:
p = 9
samplefunc(p)
msgbox p ' returns 9, because p is returned byVal
' With call:
r = 10
call samplefunc(r)
msgbox r ' returns 99, because r is returned byRef
' With call and argument protected:
s = 11
call samplefunc( (s) )
msgbox s ' returns 11, s is returned byVal
' And a last example of a function call with multiple argument with combined protection:
call multifunc( (IamProtected), IamUnprotected )
Rules in short:
A sub/function call with an argument in protected mode overrides a byRef in the function.
A sub/function call with an argument in unprotected mode is returned byRef by default unless it is overridden in the function by a byVal.
An literal or const is always returned byVal.
Syntax proposal:
OK, it is ugly, but if you use parenthesis because they are part of the call, you should use them with spaces between the first and last argument and no space between the function:
call f( a, b )
If you want to use arguments in protected mode, you should use no spaces between the parenthesis and the arguments, but do use them between the function/sub and the parenthesis belonging to the function/sub call:
f (a), (b)
or
call f( (a), (b) )
October 14, 2008
Subscribe to:
Post Comments (Atom)
1 comment:
The parentheses aren't a "protect me" - they change what is sent.
In 'samplesub (z), y', you don't pass z - you pass the result of the expression '(z)'. Similarly, if you'd done 'samplesub x+z, y', you'd be passing the result of the expression 'x+z'.
The result of the expression is then passed byval or byref as per the sub or function's definition.
Post a Comment