How to Use VBScript Variables?

2
151
Spread the love
  • 12
  • 34
  • 14
  • 12
  •  
  •  
  •  
  •  
  •  
  •  
    72
    Shares

How to Use VB Script Variables?
How to Use VB Script Variables?
How to Use VB Script Variables?

This post talks about

  • VBscript Variables
  • UFT Variables
  • vbscript how to use variables
  • How to declare ,use and call a variable

VB Script Variables:

A variable is a convenient placeholder that refers to a chunk of memory(referred by the variable name) inside large computer memory location where we can store program information (information can be anything starting from literals to String to expression outcome) that may change during the time our script is running.As computer memory is temporary the variable blocks are also temporary.

In other words,A Variable is a name given by users to assign a piece of data that is stored inside computer’s memory.Variables hold values of different data types that are specified when the variable is declared.

Declaring Variables:

A variable is live when it is declared or dimensioned even before using it.We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement.Although VBScript supports undeclared variable but it is not a good programming practice.The above said dim statement or command we are instructing computer to book or reserve some amount of memory and identify the block of memory with the variable name provided by us.

For example:

Dim city
Dim x

We declare multiple variables by separating each variable name with a comma. For
Example:

Dim x, Top, Bottom, Left, Right 

It is always better to use comments while declaring and using variables. These comments will increase readability and understand-ability later down the line.

Naming Restrictions for Variables

Variable names follow the standard rules for naming anything in VBScript. A variable name:

  • Must begin with an alphabetic character like strName but 99Name is not allowed so as &Name.
  • Variable name can contain numeric at the middle or end.
  • Variables should follow a Hungarian notation syntax.(Variables should have prefix along with the variable like-strName,strEmailId)
  • Variable can have underscore or number as postFix but any other characters are not allowed.like strStudent9 is allowed but intRoll*Number is invalid.
  • Cannot contain an embedded period or space.
  • Must not exceed 255 characters.
  • Must be unique in the scope in which it is declared.
  • Must be clear and meaningful.
  • We can use camel casing for variable declaration.
  • Variable name can not be same as reserved keywords.

Important prefix for VBScript variable or UFT variable: Hungarian notation:

Hungarian notation is naming convention frequently used in VBScript or UFT during framework development. Here we attach a datatype information as a prefix along with the variable name.

Example:

boolIsSelected- It denotes that boolIsSelected is a variable of type boolean.

Similarly we can have intSalary- It denotes that the variable intSalary if of type integer.

Notation Meaning Example
var Variant type varMyVariable
err Error type errMyError
lng Long type lngMyVariable
sng Single type sngMyVariable
int Integer type intMySalary
byt Byte type bytAccoutNumber
dbl double type dblAverage
str String type strStudentName
obj Object type objFSO
cur Currency type curAmmount
dtm Date type dtmMyDate
bool/bln boolean type boolSelected/blnSelected

However, Hungarian notation does not determine the datatype of the variable or does not define the datatype. This is still a coder’s responsibility. The actual value assigned to the variable will be checked by WSH . WSH then will determine the subtype.

Why we need to use Hungarian notation?

This will clear out ambiguity when the project becomes spirally big. Each variable will contain the type info will decrease the overhead during maintenance. So it is not a rule rather a best practice.

Scope of Variables

A scope can be thought as a boundary within which the variable is active,accessible and visible. A variable’s scope is determined by where we declare it.When we declare a variable within a procedure, only code within that procedure can access or change the value of that variable.If we declare a variable outside a procedure, we make it recognizable to all the procedures in our script. This is a script-level variable, and it has script-level scope.Once the execution flow crosses the boundary, it is said that the scope for the variable is ended.

Variable Scope:

In VBScript the scope of a variable is broadly classified into the following sections:

  • Global Scope
  • Method level Scope
  • Class Level Scope
Global Scope:

The variable is said to have Global or Script level scope ,when it is available throughout the script file. Any variable defined outside of a procedure or function is said to have Global scope.

The best practice is to create and declare all variables in the top of the script.

By Default the variables created in the .QFL(Quicktest Function library) or .VBS(VBScript) are on the main body.Hence they are global in nature.

We can not have more than 127 script level variables in Script file.We can consider script level variables has only one memory locations but they are not constants.Hence the variable value can be changed anytime from the script.

Method level Scope:

The variables declared and used inside a method or procedure or functions,can be used inside the method,procedure and function are called to have method level scope.

They are local to method and can not be accessed outside of the method.We can not have more than 127 method level variables in method.

Advantage of having Local variable?

Local variables eliminates the possibility to accidental modification of the global variables.In a real time scenario of script development, it is encouraged to create maximum number of local variables than global variables.

Class level Scope:

Class is a logical grouping of properties and methods.A class is designed inside a class block.

Class XYZ
//body of the class
End Class

A variable declared inside a class (mainly private)has the class level scope.The variable with class level scope can not be accessed outside of the class.
They are very important while designing modular or hybrid framework.

Life Time of Variables:

The lifetime of a variable depends on how long it exists.The lifetime of a variable is directly linked with the scope.The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running.At procedure level, a variable exists only as long as the procedure is live.

Assigning Values to Variables or Variable initialization:

Values are assigned to variables creating an expression as follows:
The variable is on the left side of the expression and the value you want to assign to the variable is on the right.

  • Variable initialization can be straight forward

For example:

dim A,City,X,Y
A = 200
City = "Hyderabad"
X=100: Y=200 
  • It can be complex as an output of the expression or output of dialog box or something else.

Example:

Dim City
City = InputBox("What is your city name")
Dim x
x=y+z 

Dynamic Memory Allocation of Variant:

VBScript engine(WSH) takes care of the memory allocation during runtime. WSH changes the memory allocation when needed.

Dim x
x="Hi good morning"
......
......
x="Hello again! had lunch? How can I help you?"
......
......
......
x=40
......
......
......
x=4/10/2019 

Each time WSH encounters the variable x ,depending on the size of the data or content,the engine will assign memory to the variable.If it sees that the memory needed is less than the previously used data, it will truncate the extra memory for other use.So allocation and deallocation of memory is done automatically by WSH.

Scalar Variables and Array Variables

A variable containing a single value is a scalar variable.Scalar variables are having very limited ability to handle large number of data or a data structure. A variable containing a series of values, is called an array variable.Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses () following the variable name.

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
  • 12
  • 34
  • 14
  • 12
  •  
  •  
  •  
  •  
  •  
  •  
    72
    Shares

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here