Vb Script A Stepping Stone To Learn Qtp Automation Tool}

Author:  |  Category: Shipping

VBScript a Stepping-stone to learn QTP Automation Tool

by

ExpertQTP

VBScript:

Vbscript is a scripting language developed by Microsoft. It is the simplified version of Visual Basic. It is very closely related to BASIC programming language.

What is a Variable?

A variable is a container in the computers memory which is used to store information that you want to store. It is possible to change the value of the variables inside your script. In VBScript, all variables are of one fundamental datatype: Variant which means it can store different types of data.

Naming conventions for Variables

While creating variables, you need to follow some rules. These are:

They should always begin with a letter and they should not contain a period(.) sign.

Variable names should not exceed 255 characters.

They should be unique in the scope in which they are declared.

They should not have names same as keywords like Dim, For etc.

How can I create variables with the above knowledge?

Variables can be created using Public, Private or Dim statements. The most common statement is Dim.

For an example, you can use the following syntax to declare variables.

###########

Dim var

###########

Initially this variable has no value which means its empty. After initializing a variable and assigning a value like in above case, the following expressions will evaluate to True:

If var = 0 Then

If var = Then

You can also create variables even without using Dim statement however it is not recommended to do so. Following example will clarify the concept:

Dim A

A=B+C

Is there any way to use keywords as variable names?

Yes, there is a way. Through this way we can even use keywords or some special symbols as variable names. Lets see, how it done. For this, you have to place variable names inside brackets.

Dim [For]

[For] = 20

Msgbox [For]

Here, we created a variable named [For] inside brackets which is not possible otherwise. Now the Msgbox is a predefined function inside VBScript which is used to display a dialog box to see the value of the variable. The output of the code is: 20

What is Option Explicit statement?

Option Explicit is used to force explicit declaration of all variables inside your script. This statement is always used at the top of all VBScript statements. This means, you are forced to declare all your variables using either of the statements Dim,Public or Private.

Now lets see what happens if we try to use a variable without using Dim statement in the case when Option Explicit statement is also used.

Option Explicit

A=10

MsgBox A

This is the output you will get:

#################

Variable is undefined: ‘A’

#################

Hence to avoid this error, use the following code:

Option Explicit

Dim A

A=10

MsgBox A

In order to read more articles, you can visit the site http://quicktesthp.blogspot.com

Article Source:

VBScript a Stepping-stone to learn QTP Automation Tool}

Comments are closed.