VBScript Variables

VBScript Variables (New Version Document)

Variables

Chapter Index

 a) What is Variable?

 b) How to declare Variables in VBScript?

 c) Implicit & Explicit Declaration of Variables


 d) Option Explicit Statement

 e) Assigning values to Variables

 f) Purpose of Variables

 g) Naming Restrictions

 h) Scope of Variables

 i) Types Variables

 j) Array Variables

 k) Dynamic Arrays, Dimensional Arrays

 l) Assigning series values to Array Variables

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

a) What is Variable?

Definition 1:
A variable is a convenient placeholder that refers to a computer memory location where we can store program information that may change during the time our script is running.

Definition 2:
A named memory Location for storing the Data.

Variables store in Primary Memory (RAM)

Primary Memory: RAM

Secondary Memory:  HDD, CD ROM, USB etc...

b) How to declare Variables in vbscript?

We can use either Public or Private or Dim Statements for declaring variables.

Generally we use Dim Statement for declaring variables.

Example:

Dim a
Dim b
Dim c
-----------

Dim a, b, c

Note: We can declare multiple variables in a statement by separating with comas, otherwise we can use multiple statements.

c) Implicit & Explicit Declaration of Variables

VBScript supports Implicit and Explicit declaration of variables, but declaring variables explicitly is the best practice.

Dim num1

num1 = 100 'Explicit variable

num2 = 200 'Implicit variable

Msgbox "Addition of num1, num2 is: "&num1+num2

In the above example num1 is Explicit variable, first we declared then used.

num2 is Implicit  variable, we used num2 without declaration.

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

d) Option Explicit Statement

 Option Explicit statement forces declaration of all variables in the Script, and it should be the first statement. Using this statement we can avoid misspell problems.

 Ex:
-----

Option Explicit

Dim Tickets, Price, Total

Tickets = 5

Price = 120

Total = Tickets * Priee

Msgbox Total

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

e) Assigning values to Variables
In Computer programming value assignment is 2 types:

1) Initialization
 Assigning values directly in the Script / Program

2) Reading
Reading from Input devices, from files, from Databases, and Reading from Application Objects.



Example:

Dim num1, num2

num1 = 100 'Initialization

num2 = InputBox("Enter num2") 'Reading

Msgbox num1 + num2

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

f) Purpose of Variables

1) 'Holding the data
------------------------
Dim a, b, c

a= 100 'Holding the data

a = 10

b=3

2) 'Store the value that returned by a program
-------------------------------------------------------- 

c = a ^ b 'Store the value that returned by a program

Msgbox c '1000

3) 'Store  the value that returned by a Function
------------------------------------------------------

 c= Date 'Store  the value that returned by a Function

 Msgbox c

4) 'Storing Object Reference
---------------------------------

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

 5) Passing as Parameter
----------------------------------
Dim orderNo

For orderNo = 1 to 5 Step 1

        Window("Flight Reservation").Activate

        Window("Flight Reservation").WinButton("Button").Click

        Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"

        Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set orderNo

        wait 2

        Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click

Next

g) Naming Restrictions



a) Should start with Alfa bytes

Dim abc 'Correct

Dim a9 ' Correct

Dim ab c 'Incorrect

Dim 9ab 'Incorrct

b) Should not contain embedded periods

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 char in length


d) Must be unique in the scope of declaration


Dim a, b, c

Dim d, e, f

Dim g, h, A 'Incorrect

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


Don't use Reserved words


Dim Wait

Wait =100

Msgbox Wait

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


h) Scope of Variables

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

a) Program / Script Level Variables

b) Function / Procedure Level Variables

Script Level Variables can be used for entire script, whereas Function level variables can be used within the Function only.

Example:

Dim a, b, c 'Script Level

a= 10

b=20

c= a +b

Msgbox c 'Output 30

Function xyz()

   Dim d, e 'Function Level

   d=40

   e= a + d

   Msgbox e 'Output 50

End Function

Call xyz()

Dim f, g 'Script Level

f= 70

g= b + d + f '(20 + 0 +70)

Msgbox g '90

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

Option Explicit

Dim a, b, c 'Script Level

a= 10

b=20

c= a +b

Msgbox c '30

Function xyz()

   Dim d, e 'Function Level

   d=40

   e= a + d

   Msgbox e '50

End Function

Call xyz()

Dim f, g 'Script Level

f= 70

g= b + d + f

Msgbox g 'Error

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

 i) Types Variables


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

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


j) Array Variables


Dim a, b(3)

b(0) = "India"

b(1) =100

b(2) =10.234

b(3) =40


a = b(1) + b(3)

Msgbox a '140

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

Dim a, b(3)

b(0) = "India"

b(1) =100

b(2) =10.234

b(3) =40

b(4) = 50 'Error



a = b(1) + b(3)

Msgbox a '140


k) Dynamic Arrays, Dimensional Arrays

Dim a, b(3), c()

ReDim c(3)

c(0) =10

c(1) =20

c(2) =30

c(3) =40

a= c(1) + c(3)

Msgbox a '60



ReDim c(5)

c(4) = 50

c(5) = 60

a= c(2) + c(5)

Msgbox a '60

 Preserve Keyword
If we use preserve keyword it carry's existing data as it is.
----------------

Dim a, b(3), c()

ReDim c(3)

c(0) =10

c(1) =20

c(2) =30

c(3) =40

a= c(1) + c(3)

Msgbox a '60


ReDim Preserve c(5)

c(4) = 50

c(5) = 60

a= c(2) + c(5)

Msgbox a '90

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

Dim a, b(3), c(), d(4, 5)

d(0, 0) ="QTP"



l) Assigning series values to Array Variables

a) Using Array Function


Dim a

Msgbox IsArray(a) 'False

a = Array("India",100, 100.34, #10/10/2010#)

Msgbox IsArray(a) 'True

Msgbox a(1) '100


b) Using Split Function

Dim a, b

a= "VB@Script@Language"

Msgbox IsArray(b) 'False

b= Split(a,"@")

Msgbox IsArray(b) 'True

Msgbox b(1) 'Script


Dim a, b

a= "VB Script Language"

Msgbox IsArray(b) 'False

b= Split(a)

Msgbox IsArray(b) 'True


Msgbox b(1) 'Script
-----------------------------------------------
Note: In Perl script 3 types of variables available, 1) Scalar Variables, 2) Array Variables, 3) Associated Arrays, but in VBScript 2 types variables only.

VBScript Provided Dictionary Object, it is equivalent to Associated Array in Perl Script
----------------------------------------------------------------------------------------- 

0 comments:

Post a Comment