VB Script Variables

VBScript Variables
Definition 1):
Variable is a named memory location for storing program information
Definition 2):
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.
Purpose of Variable:

a) Comparing values

Example:
Dim x,y,a
x=100
y=100
a=x=y
Msgbox a 'It returns True

b) Holding Program Result

Example:
Cost=Tickets*Price

c) Passing parameters

d) To store data that returned by functions
Example:
myDate=Now ‘ It returns current data & time

e) To hold data
Example:
myName=”gcreddy”

Declaring Variables

We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement.
For example:
Dim city
Dim x
We declare multiple variables by separating each variable name with a comma. For
Example:
Dim x, y, city, gcreddy

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

The Option Explicit statement should be the first statement in our script.


Option Explicit Statement

Forces explicit declaration of all variables in a script.

Option Explicit ' Force explicit variable declaration.
Dim MyVar ' Declare variable.
MyInt = 10 ' Undeclared variable generates error.
MyVar = 10 ' Declared variable does not generate error.
Naming Restrictions for Variables

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

a) Must begin with an alphabetic character.
Dim abc    'Right
Dim 9ab    'Wrong
Dim ab9    'Right

b) Cannot contain an embedded period.
Dim abc     'Right
Dim ab.c    'worng
Dim ab-c    'wrong
Dim ab c    'wrong
Dim ab_c   'Right

c) Must not exceed 255 characters.

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

Scope of Variables

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.
Example:
Dim x,y,z
x=10
y=20
z=x+y
msgbox z    'Returns 30
Function res
   Dim a,b,c
   a=30
   b=40
   c=a+b+y
   msgbox c   ' Returns 90
End Function
Call res

Life Time of Variables

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 procedure level, a variable exists only as long as you are in the procedure.
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:
A = 200
City = “Hyderabad

X=100: Y=200
Scalar Variables and Array Variables

A variable containing a single value is a scalar variable.

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.

Example:
Dim A(3)

Although the number shown in the parentheses is 3, all arrays in VBScript are zero-based, so this array actually contains 4 elements.

We assign data to each of the elements of the array using an index into the array.
Beginning at zero and ending at 4, data can be assigned to the elements of an array as follows:

A(0) = 256
A(1) = 324
A(2) = 100
A(3) = 55

Similarly, the data can be retrieved from any element using an index into the particular array element you want.

For example:

SomeVariable = A(4)

Arrays aren't limited to a single dimension. We can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions.

In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:

Dim MyTable(5, 10)

In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.
Dynamic Arrays

We can also declare an array whose size changes during the time our script is running. This is called a dynamic array.

The array is initially declared within a procedure using either the Dim statement or using the ReDim statement.

However, for a dynamic array, no size or number of dimensions is placed inside the parentheses.

For example:
Dim MyArray()
ReDim AnotherArray()

To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.

In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.

ReDim MyArray(25)

ReDim Preserve MyArray(30)

There is no limit to the number of times we can re size a dynamic array, although if we make an array smaller, we lose the data in the elimination

1 comments:

It is great post, with simple language and in detail.

Post a Comment