Friday, August 5, 2011

QTP/VB Scripting - Using ByRef & ByVal methods for passing variables to Functions

There are two ways of Passing variables to a function or subroutine from your main script. They are Byval and ByRef.

This post would explain about both of these ways with sample examples.

ByVal:

This method passes the value of the variable to the function or subroutine. If the value of the variable has been changed in your function, then these changes are available till you are in that function. But once the cursor returns to the main script then these changes wouldn't be passed back to the main script.

Please see below example:

Dim sVariable

sVariable=10

GetByValmethodValue sVariable
msgbox "Value of the Variable after returning from the function is: " & sVariable

Public Function  GetByValmethodValue(ByVal sValVariable)
sValVariable=100
msgbox "Value of the Variable inside the function is: " & sValVariable
End Function


In the above example, the value in the variable 'sVariable' is copied to 'sValVariable' while it is passed to the function as it is passed using ByVal method. As a result, no matter what changes are being done to 'sValVariable', they wouldn't be passed back to 'sVariable' as both are two different variables now.

ByRef:

This method passes a reference to the variable to the function. As a result this method passes back the changed value of the Variable to the main script. Meaning, if the value of the variable is changed in the function, then the changed value would be passed back to the main script.

Please see below example:

Dim sVariable

sVariable=10

GetByRefmethodValue sVariable
msgbox "Value of the Variable after returning from the function is: " & sVariable

Public Function  GetByRefmethodValue(ByRef sRefVariable)
sRefVariable=100
msgbox "Value of the Variable inside the function is: " & sRefVariable
End Function




In the above example, the value in the variable 'sVariable' is passed a reference to 'sRefVariable' and as a result, all the changes that are being done to 'sRefVariable' would be passed back to 'sVariable'.

Default method that is used if you don't specify either ByVal or ByRef:

The default method that is going to be used if you don't specify either ByVal or ByRef depends on how you are calling the function when there is only one variable being passed. If you are passing multiple variables, then the default method would always be ByRef. Please have a look at following samples to get better Idea.


When passing only one Variable:

Sample 1: Without parentheses around the variable while calling the function



 Sample 2: With parentheses around the variable while calling the function



If a parentheses is present around the variable while calling the function, then it would be passed as a ByVal variable by default.

If there is no parentheses around the variable while calling the function, then it would be passed as a ByRef variable by default.

When passing multiple Variables:

Sample 3: With parentheses around the variables




Note: While calling a function with Parentheses around the variables when they are more than 1, a Call statement needs to be used before the function.

Sample 4: Without parentheses around the variables

 

While passing multiple variables in a function, no matter whether they have parentheses around the variables or not, the default method that is going to be used is always ByRef.

No comments:

Post a Comment