How to Use a Parentheses in QTP or UFT?

0
141
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Parentheses or Cannot use parentheses when calling a Sub or Call a Function in UFT
Parentheses in UFT or VBScript

Using of parenthese in UFT/QTP or VBscript is different than other programing languages. This post talks about how to Use Parentheses in UFT or QTP,How to Call a Function in UFT,how to Call a subroutine in UFT. After reading this post I hope you will be clear on where to use parentheses and where not to use parentheses.This post also tells why the issue-“Cannot use parentheses when calling a Sub” comes and How to resolve Cannot use parentheses when calling a Sub error.

What is the meaning of Parentheses in VBScript or UFT?

Parentheses are mostly used in subroutine calls,function calls,array declarations,array accessing, evaluating part of expression,passing value by ByVal or ByRef.

Lets see theses example one by one.

Parentheses in Subroutine and Functions

General Idea about parenthese

To achieve the desired result and to avoid errors, it is important that you use parentheses () correctly in your statements. For more information, see below…
You must use parentheses around method arguments if you are calling a method that returns a value and you are using the return value. For example, use parentheses around method arguments if you are returning a value to a variable, if you are using the method in an If statement, or if you are using the Call keyword to call an action. You also need to add parentheses around the name of a checkpoint if you want to retrieve its return value.
Tip: If you receive an Expected end of statement error message when running a step in your test, it may indicate that you need to add parentheses around the arguments of the step’s method.
Following are several examples showing when to use or not use parentheses.

Impact of parenthese on Subroutine

A subroutine is a group of statements that does not return any value. A subroutine may or may not take arguments. So based on arguments , subroutines can be classified into two broad categories:

  1. Subroutine with arguments
  2. Subroutine without arguments

Let’s look into the structure of subroutines with argument

Sub MySub(sName,intTotal)
//body of the sub
End Sub

In this case (Subroutine) since it is accepting arguments, they need to be inside parentheses. This is the standard structure syntax of Subroutine.

Let’s look into the structure of subroutines without argument

Sub MySub
//body of the sub
End Sub

How to call the Subroutine?

There are two ways to call a subroutine. They are as follows:

  1. Directly by name.
  2. Using VBScript’s call keyword
Calling directly by subroutine name

While calling a subroutine by its name, we do not need to pass any argument, so we do not need any parenthese.

Example:

MySub

For calling subroutines with arguments, we still do not need parenthese. The arguments are separated by comma(,).

Example:

MySub "Test",10
or
MySub sName,intTotal

In case you call a subroutine with parenthese,you will get runtime error “Cannot use parentheses when calling sub”.This is the main reason why we get Cannot use parentheses when calling a Sub issue.

Using VBScript’s Call keyword to call a subroutine

The use of keyword ‘call’ is purely optional when we to call a subroutine. Call keyword is only applicable when the subroutine accepts arguments and they are enclosed in parentheses. In case our subroutine does not accept any argument, there is no need to use parentheses.

Example:

Call MySub
Call MySub(sName,intTotal)
Call MySub("Test",10)

Parenthese on Function:

Let’s look into function now. function is a group of statements that can return some value. There are two possible ways we can call a function.

  1. By using name
  2. By Using VBScript keyword-Call
Calling a function by using directly Name

When we call a function that does not have an argument or any return value, we can use the followings

MyFunc
MyFunc()

In case the function has some argument and does not return any value we can use the following:

MyFunc sName,intTotal
MyFunc "Test",10

Scenario changes when our function has some return value:

val=MyFunc
val=MyFunc()
val=MyFunc(sName,intTotal)
val=MyFunc("Test",10)

In this case, If you have one or more arguments, then it is a must to use parentheses.

Using Call keyword to call a function

If a function has arguments,you must need to put parentheses while using call keyword. the arguments are separated by commas.

Note: If you use Call keyword to call a function, it can not return a value.

Example

Call MyFunc(sName,intTotal)
Call MyFunc("Test",10)

This is how we Call a subroutine in UFT.

Parenthese on Array

Parentheses can be used on Arrays in VBScript.

Dim myArray(3) ' will create an array of size-4

We can access any array element by using parenthese, like

Val=myArray(0)

Impact of Parentheses on Expression

Parentheses can be used to change the priority of an arithmetic expression. Like

(2+4)*3 evaluates 18 instead of 14

Impact on ByRef and ByVal

VBScript supports ByRef and ByVal keywords to specify the parameters passed either by reference or by value. When arguments are passed by value,any change inside the function/sub will not carried to outside of the scope.

On the other hand, if arguments are passed by reference, the changes to the variable becomes permanent and are available outside of the functions/Subs.

By default,all values are passed by reference.

Sub MySub(y) //by default pass by reference
y="Test default"
End Sub
Sub MySub(ByVal y) 
y="Test By val"
End Sub
Sub MySub(ByRef y) 
y="Test by ref"
End Sub
y="hello"
MySub(y)
msgbox y
MySub(ByVal y)
msgbox y
MySub(ByRef y)
msgbox y

Test these and let me know in comment what is your output.

How to Use a Parentheses in QTP or How to Use a Parentheses in UFT?

To achieve the desired result and to avoid errors, it is important that you use parentheses () correctly in your statements. For more information, see below…
You must use parentheses around method arguments if you are calling a method that returns a value and you are using the return value.

For example, use parentheses around method arguments if you are returning a value to a variable if you are using the method in an If statement, or if you are using the Call keyword to call the action. You also need to add parentheses around the name of a checkpoint if you want to retrieve its return value.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here