How to Use VBScript Variables?

Table of Contents

Share and Enjoy !

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:

Memory is a temporary working space for computer hence they are transient. Transient refers to the term that is things that are placed in memory are not stored there permanently. Variables use these memories to hold values. Hence variables are also transient.

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 the 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 the computer to book or reserve some amount of memory and identify the block of memory with the variable name provided by us.

For example:

im 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.
  • The 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.
  • The 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 datatype information as a prefix along with the variable name. A data type prefix can tell the programmer what type of data the coder intend to use for the variable.

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 do 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 a 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 at the top of the script.

Dim is generally used to declare script or global level variables. Dim can be used to declare variables at script level or procedure level or class level. Variables declared in script level irrespective of declaration(Dim, public, private) are available inside the script.

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

We can not have more than 127 script level variables in the 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 the method-level scope.

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

We need to declare variables inside the procedure. Private or public are not allowed as access modifiers.

Advantages of having a Local variable?

Local variables eliminate the possibility of accidental modification of the global variables. In a real-time scenario of script development, it is encouraged to create the 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 a class-level scope can not be accessed outside of the class. So variables declared in class level with dim acts as a public variable.
They are very important while designing a 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 the procedure level, a variable exists only as long as the procedure is live.

Private or script level variables are available throughout the script irrespective of the declaration. Any variable declared in class as private has scope only class level. they are private to the class.

A variable declared as public in class level scope becomes a public property of the class. However, in general concept, these are not that effective. They are useful while creating public property of a class which is not a best practice.

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 a 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 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 seems 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 are done automatically by WSH.

Literals

literals are referred to as static data that resides in our Code. ( like- numbers, data’s, Boolean values, etc. It is just the opposite of a variable. Literals can not change during the lifetime of the script.

Literals are also a piece of static data in your code which is stored in a variable but not a named constant. Named constant is similar to variable concept but its value can not be changed during code execution time. The intention of using the literal is not to change the variable where it is saved. (Just the opposite of the meaning of variable).
exp-


Dim myMessage
myMessage="good morning"

Use of named constant:-


Option Explicit 
Dim varTime
Const GREETING1="Good morning" 
Const GREETING2="Good noon" 
Const GREETING3= "Good evening" 
varTime= getTime( now)
If (varTime>= 6) and(varTime<=12) then msgbox GREETING1 Else if (varTime>=12) and(varTime<=16) then 
msgbox GREETING2
Else
msgbox GREETING3
End If

The literals can be String, text, numbers, dates Boolean, etc.

The below section is kind of repetitive but useful when you work with UFT variables:

1. Variable: Variables are a container that holds value. Before using any variables we need to declare them. (This is a best practice.) We need to use a dim statement to declare. The variable declaration is nothing but allocate some amount of memory for that variable to hold some value.
Like: dim c_ball,d_count
Again this declaration can be of three types as per scope:
1. Dim Statement
2. Public Statement
3. Private Statement
Dim can be accessible from the entire script.
Public statement variables are available to all procedures in all scripts.
Private statement variables are available only to the script in which they are declared
There is a separate term called constant.
Like:
Const variable_Name=”I am a good boy”
Now, this variable_Name can be used anywhere. And its value is the same. The scope is public by default.
VBScript in itself has a number of defined intrinsic constants like vbOK, vbCancel, vbTrue, vbFalse and so on

You can also declare a variable implicitly by simply using its name in your script. That is not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require an explicit declaration of all variables. The Option Explicit statement should be the first statement in your script.

Naming Restrictions

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

1.Must begin with an alphabetic character.
2.It cannot contain an embedded period.
3.Must not exceed 255 characters.
4.Must be unique in the scope in which it is declared.

Scope and Lifetime of Variables

A variable’s scope is determined by where you declare it. When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is a procedure-level variable. If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a script-level variable, and it has a script-level scope.

The lifetime of a variable depends on how long it exists. The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running. At the procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed. Local variables are ideal as temporary storage space when a procedure is executing. You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.

Assigning Values to Variables

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. For example:

B = 200

Scalar Variables and Array Variables

A variable containing a single value is a scalar variable. Scalar variables are having very limited ability to handle a large number of data or a data structure.

Read more about Array.

The important difference between VBScript and VBA is, VbScript is an untyped language which means that all variables are variants and don’t have an explicit type (like-integer or string or Boolean).

Share and Enjoy !

READ  VBScript Operators

3 thoughts on “How to Use VBScript Variables?”

  1. Hello! I’m at work browsing your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the excellent work!

  2. Greetings! I know this is somewhat off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having trouble finding one? Thanks a lot!

  3. Pingback: Google

Leave a Comment

Your email address will not be published. Required fields are marked *