VBScript Variables

VBScript Variables, Implicit Declaration of Data Types, What is VBScript Variable?, Assign Values to Variables, and Declare & Use Variables.

VBScript Variables are variants, they can hold any type of data throughout the program. VBScript supports the implicit and explicit declaration of variables.

Variables in VBScript

1. What is a Variable?
2. Declaration of variables
3. Implicit and Explicit variables.
4. Assigning Values to Variables
5. Purpose of Variables
6. Variable Naming Restrictions
7. Scope of Variables
8. Types of Variables

1. What is a Variable?

A named memory location to store the data.

In a computer environment, there are two types of memory

i) Primary memory: RAM

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

> Variables store in Primary Memory (RAM).

2. Declaration of variables

> using either Public or Private or Dim statements, we can declare Variables.

Ex:

Dim a
Dim x, y, z
Or
Dim x
Dim y
Dim z

3. Implicit and Explicit variables.

Ex:
Dim a
a = 100 ‘Explicit
b = 200 ‘Implicit
Msgbox a + b
———————
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee
Msgbox Total ‘0 (Incorrect output)
———–
Option Explicit
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee
Msgbox Total ‘Error

Option Explicit – It forces declaration of all variables in a Script

4. Assigning Values to Variables

Assigning Values to Variables is two types

i) Initialization

ii) Reading

Example:
Dim num1, num2
num1 = 100 ‘Initialization
num2 = InputBox (“Enter Num2 value”) ‘ Reading
Msgbox num1 + num2

5. Purpose of Variables

Dim a, ord
‘Holding the data
a = 100
Msgbox a
‘Storing the data that returned by a Function
a = Now
Msgbox a
‘Storing the data that returned by a program
a = (10 ^ 3)*2
Msgbox a

‘storing object Reference
Set a = CreateObject(“Scripting.FileSystemObject”)

‘As parameter
For ord = 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 ord
Wait 2
Window(“Flight Reservation”).Dialog(“Open Order”).WinButton(“OK”).Click
Next

6. Variable Naming Restrictions

i) Variable names should start with Alfa bytes

Ex:
Dim abc ‘Correct
Dim a12 ‘Correct
Dim 1ab ‘Incorrect

ii) Should not contain embedded periods

Ex:
Dim abc ‘Correct
Dim ab c ‘Incorrect
Dim ab.c ‘Incorrect
Dim ab*c ‘Incorrect
Dim ab_c ‘Correct
—-
iii) Must not exceed 255 characters

1 to 255
———-
iv) Must be unique in the scope of the declaration.

Dim a, b, c ‘Correct
Dim d, e ‘Correct
Dim f, g, A ‘Incorrect

7. Scope of Variables

i) Script Level Variables

They can be used for the entire script.

ii) Function Level Variables

They can be used within the Functions only

Example:
———–
Dim a, b, c ‘Script level variables
a = 10
b = 20
c = a + b
Msgbox c ’30
Function xyz()
Dim d, e ‘Function level variables
d = 40
e = a + d
Msgbox e ’50
End Function
Call xyz()
Dim f, g ‘Script level variables
f = 70
g = b + d + f
Msgbox g ’90
——————————
Option Explicit
Dim a, b, c ‘Script level variables
a = 10
b = 20
c = a + b
Msgbox c ’30
Function xyz()
Dim d, e ‘Function level variables
d = 40
e = a + d
Msgbox e ’50
End Function
Call xyz()
Dim f, g ‘Script level variables
f = 70
g = b + d + f
Msgbox g ‘Error

8. Types of Variables

i) Scalar Variable

To store one value at a time, Value may vary throughout the Execution.

ii) Array Variable

To store series of values at a time based on size of the Array.

Declare Array variable:

Dim a, b(3)
b(0) = 10
b(1) = “abcd”
b(2) = 1.34
b(3) = 40
a = b(0) + b(3)
Msgbox a ’50
——————
Dim a, b(3)
b(0) = 10
b(1) = “abcd”
b(2) = 1.34
b(3) = 40
b(4) = 50 ‘Error
a = b(0) + b(3)
Msgbox a ’50
———————————-
Dynamic Array

Dim a, b(3), c()
ReDim c(3)
c(0) = 10
c(1) = 20
c(2) = 30
c(3) = 40
a = c(1) + c(2)
Msgbox a ’50
ReDim c(5)
c(4) = 50
c(5) = 60
a = c(3) + c(5)
Msgbox a ’60
————————
Use Preserve keyword

Dim a, b(3), c()
ReDim c(3)
c(0) = 10
c(1) = 20
c(2) = 30
c(3) = 40
a = c(1) + c(2)
Msgbox a ’50
ReDim Preserve c(5)
c(4) = 50
c(5) = 60
a = c(3) + c(5)
Msgbox a ‘100
————————————
Dimensional Arrays
———————-
Dim a, b(3), c(), d(4, 5)
d(0, 0) = 10

Assigning series of Values at a time to Array variables

a) Using Array Function

Dim a
Msgbox IsArray(a) ‘False
a = Array(100, “VBScript”, 20, 30, 40, #10/10/2010#)
Msgbox IsArray(a) ‘True
Msgbox a(1) ‘VBScript
Msgbox UBound(a) ‘5

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
Msgbox UBound (b) ‘2
———————-
Dim a, b
a = “VB,Script,Language”
msgbox isArray(b) ‘False
b = Split(a, “,”)
Msgbox IsArray(b) ‘True
Msgbox b(1) ‘Script
Msgbox UBound (b) ‘2
————————–
Dim a, b
a = “VB@Script@Language”
msgbox isArray(b) ‘False
b = Split(a, “@”)
Msgbox IsArray(b) ‘True
Msgbox b(1) ‘Script
Msgbox UBound (b) ‘2
—————————-
Dim a, b
a = “VB@%$Script@%$Language”
msgbox isArray(b) ‘False
b = Split(a, “@%$”)
Msgbox IsArray(b) ‘True
Msgbox b(1) ‘Script
Msgbox UBound (b) ‘2
——————————–
Note: In Split Function default delimiter is space, we can use any value as a delimiter, but we need to specify the delimiter.z

VBScript Tutorial

VBScript Videos

Follow me on social media: