VBScript Variables

VBScript Variables

I) What is Variable?

Definition 1: 


A named memory location to store the Data.

Definition 2:  

A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the time your script is running.
 
Variables store in Primary memory -RAM (Random Assess Memory)


Primary memory - RAM

Secondary memory - HDD, CD-ROM, DVD, USB Drive etc...

II) Declaration of Variables

Using either Public or Private or Dim statement we can declare variables.


Web developers use Public ar Pribate Statements but in our Test Automation we use Dim statement only.

Syntax:

Dim Variable1Name, Variable2Name, Variable3Name
Dim Variable4Name

Dim a
Dim b
Dim c

Or


Dim a, b, c


Note: We can declare multiple variables in a statement by seperating with commas, if you take new line again you to declare Dim statement.
-----------------------------------------------

III) Implicit and Explicit Declaration of Variables
 

First declaring then using is called explicit declaration of variable


Without declaration directly using is called implicit declaration of variables
 

Declaring variables is the best practice.

Example:
 
Dim a
a = 100 'Explicit variable
b = 200 'Implicit variable

Msgbox "Addition of a, b is: "& a + b
-----------------
Option Explicit 'It forces declaration of all variables in a Script
Dim Tickets, Price, Total
Tickets = 6
Price = 100
Total = Tickets * Priee 'Variable Undefined
Msgbox Total

------------------------------------------------
IV) Assigning values to Variables

Assigning values to variables is 2 types

a) Initialization

b) Reading


Ex:
Dim num1, num2
num1 = 100 'Initialization
num2 = InputBox ("Enter num2 value") 'Reading

Msgbox num1 + num2


Initialization: Assign value directly in the script or program  

Reading: 
           Read from Input devices
           Read from Files
           Read from Databases
           Read from Application Objects
------------------------------------------------------------
V) Usage of Variables
 

Example:
 

Dim val, OrderNumber

val = 100 'Holding the Data

val = (2 + 3)^4 'assigning a value that returned by a Program
Msgbox val

val = Date ''assigning a value that returned by a Function
Msgbox val

'Storing Object Reference
Set val = CreateObject("Scripting.FileSystemObject")

'use as Parameter
For OrderNumber = 1 To 10
Window("Flight Reservation").Activate @@ hightlight id_;_196788_;_script infofile_;_ZIP::ssf1.xml_;_
Window("Flight Reservation").WinButton("Button").Click @@ hightlight id_;_723344_;_script infofile_;_ZIP::ssf2.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" @@ hightlight id_;_199102_;_script infofile_;_ZIP::ssf3.xml_;_
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNumber @@ hightlight id_;_264558_;_script infofile_;_ZIP::ssf4.xml_;_
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click @@ hightlight id_;_592290_;_script infofile_;_ZIP::ssf5.xml_;_
Next
------------------------------------------------------------------------------
VI) Naming Restrictions
 

a) Must begin with an alphabetic character.

Dim abc 'Correct
Dim a9 'Correct
Dim 7bc 'InCorrect

b) Cannot contain an embedded Period


Ex:
Dim abc 'Correct
Dim ab c 'InCorrect
Dim ab.c 'InCorrect
Dim ab*c 'InCorrect
Dim ab-c 'InCorrect
Dim ab_c 'Correct

c) Must not exceed 255 characters (Variable size minimum 1 maximum 255 Char)

d) Must be unique in the scope in which it is declared.

Example :

Dim a, b, c 'Correct
Dim d, e, f 'Correct
Dim g, h, A 'InCorrect

Standard- Do not use Reserved words
Dim Wait
Wait = 100
Msgbox Wait
------------------------------------------------

VII) Scope of Variables

a) Program / Script Level Variables

b) Function Level Variables


Script Level Variables can be used for entire script where as Function Level variable used within the Function Only
 

Example:
Dim a, b, c ' script Level variables
a = 10
b= 20
c = a + b '(10 + 20)
Msgbox c '30
Function xyz()
Dim d, e ' Function Level variables
d = 40
e = b + d  '(20 + 40)
Msgbox e '60
End Function
Call xyz()
Dim f, g ' script Level variables
f = 70
g = a + d + f '(10 + 0 + 70)
Msgbox g '80
----------------
 
Example2:

Option Explicit
Dim a, b, c ' script Level variables
a = 10
b= 20
c = a + b '(10 + 20)
Msgbox c '30
Function xyz()
Dim d, e ' Function Level variables
d = 40
e = b + d  '(20 + 40)
Msgbox e '60
End Function
Call xyz()
Dim f, g ' script Level variables
f = 70
g = a + d + f
Msgbox g 'Error
----------------------------------------------------
VIII) Types of Variables
 

a) Scalar Variables - for storing one value at a time

b) Array Variables - for storing series of values at a time based on size of the Array

Example:


Dim a, b(3)
b(0) = 10
b(1) = "abcd"
b(2) = 30
b(3) = 40

a = b(0) + b(3)
msgbox a '50
----------
Example 2: 


Dim a, b(3)
b(0) = 10
b(1) = "abcd"
b(2) = 30
b(3) = 40
b(4) = 50 'Sub script out of range
a = b(0) + b(3)
msgbox a
------------------------------------------------------------------------------I

X) Array Variables
 

    Dynamic Arrays

Dim a, b(3), c()
ReDim c(3)
c(0) = 10
c(1) = 20
c(3) = 40

a= c(0) + c(1)
Msgbox a '30
ReDim c(5)
c(4) = 50
c(5) = 60
a= c(3) + c(5)
Msgbox a '60

------------
Dim a, b(3), c()
ReDim c(3)
c(0) = 10
c(1) = 20
c(3) = 40

a= c(0) + c(1)
Msgbox a '30
ReDim Preserve c(5)
c(4) = 50
c(5) = 60
a= c(3) + c(5)
Msgbox a '100
-------------------------------

X)  Dimensional Arrays

 Dim a, b(3), c(), d(4, 5)
d(0, 0) = "QTP"
-----------------------------------------------
XI) Assigning series values at a time

a) using Array Function

Dim a
Msgbox IsArray(a) 'False
a = Array("India", 100, 200, 10.234, #10/10/2010#)
Msgbox IsArray(a) 'True
Msgbox UBound(a) '4
Msgbox a(1) ' 100
--------------------------------------------

b) using Split Function

Dim a, b
a = "VB Script Language"
b = Split(a)
Msgbox IsArray(b) 'True
Msgbox b(0) 'VB
Msgbox b(1) 'Script
Msgbox b(2) 'Language

---------------------
Dim a, b
a = "VB@Script@Language"
b = Split(a, "@")
Msgbox IsArray(b) 'True
Msgbox b(0) 'VB
Msgbox b(1) 'Script
Msgbox b(2) 'Language
----------------------------
Dim a, b
a = "VB@#$Script@#$Language"
b = Split(a, "@#$")
Msgbox IsArray(b) 'True
Msgbox b(0) 'VB
Msgbox b(1) 'Script
Msgbox b(2) 'Language

--------------------------------------------------------------------------

0 comments:

Post a Comment