1: Declaring Variables
VBScript for QTP
Adding Comments to Your VBScripts
· You can add comments to your scripts in either of two ways. Your first option is the Rem statement.
Rem Script 2.4—Documenting your script with comments MsgBox "This script was documented using the REM statement"
· Alternatively, you can add comments to your scripts using the single quote character.
' Script 2.5 - Documenting your script with comments
MsgBox "This script was documented using the quotation character"
Working with Constants
A constant contains a value that never changes. For example, the value of PI is a constant. Use constants to define values that you know will never change during the execution of your script.VBScript also allows you to define your own constants.For example, you can define a constant that contains the name and path of a Windows folder as shown here.
Const cDefaultFolder = "C:\Temp"
In this example the name of the constant is cDefaultFolder and its statically assigned value is C:\Temp. Because this is a text value, it must be enclosed inside quotation marks. You need to leave off the quotation marks when defining constants with numeric values as shown here.
Const cMaxValue = 100
If you create a constant with a date value then you’ll need to use the following format.
Const cFixedDate = #01-05-02#
Variable Declaration
Before you can use variables in your VBScripts you should declare them. You can declare a variable in a VBScript using either of two options. The first option is to simply reference the variable in your code.
userName = "Kuldeep"
However, the preferred way to incorporate variables into your VBScripts is to formally declare them using the Dim keyword. Using the Dim keyword makes your scripts easier to read. Here is the syntax required to use Dim.
Dim variableName
variableName is the name of the variable being declared. So to use Dim to declare the variable shown earlier you would write.
Dim userName
After you have declared the variable, you can assign a value to it as shown here.
userName = "Kuldeep"
If you have a script that will use a large number of variables, VBScript lets you declare more than one at a time by separating the variables with commas.
Dim userName, userBirthday, userAge, userId
| Const | Defines a VBScript constant |
| Dim | Defines a VBScript variable or array |
| Private | Defines a VBScript variable that is only available within the script that declares it |
| Public | Defines a VBScript variable that is global in scope throughout a script and which can be accessed from other scripts |
| ReDim | Defines a dynamic array |
Examining Variable Scope
This is an example of a variable with a global scope. A global scope allows a variable’s value to be accessed from any location in the script. A variable with a local scope, on the other hand, can only be accessed within the scope that it was created. In VBScripts, local variables can be created only within procedures. VBScript supports two types of procedures—subroutines and functions—both of which are covered in greater detail later in the morning. For now I’ll just say that a procedure is a collection of VBScript statements that can be called and executed from any point in the script. Procedures are used in VBScripts as a way to improve a script’s organization.The following example shows a WSH VBScript that defines both local and global variables.
‘ * Script Name: Script 1.vbsOption ExplicitDim userNameuserName = “Kuldeep”‘ *********** Main processing section ************
WScript.Echo “Global Scope = ” + userName
Display_Names()
WScript.Echo “Global Scope = ” + userName
‘ ********* Subroutines & Functions go here ******
Function Display_Names()
Dim userName
userName = “Sharma”
WScript.Echo “Local Scope = ” + userName
End Function
The script starts off with the Option Explicit option, which forces the formal declaration of all variables. Next a variable called userName is declared and then assigned a value. Because this variable is declared outside of a procedure, it is global in scope. Next, the value of this variable is displayed.The script then calls a function named Display_Names(), which executes, displays a message, and then returns processing control back to the statement that follows the statement that called it.
Very nice article for the starters in qtp.
this article is really very nice and ,,new learner can understand this…
i want to request that plz can i get this articles by email….
its.abha.tiwari@gmail.com.
i would be thankful fo this.