Tuesday, May 4, 2010

VB Script - 1

What is VB scripting?

• VBScript is a scripting language.
• A scripting language is a lightweight programming language.
• VBScript is a light version of Microsoft's programming language Visual Basic.

How VB script works with your OS?

When a Script arrives on your computer. WSH(Windows Script Host) takes care of it.
It is a Windows administration tool. WSH creates an environment for hosting scripts.
It makes objects and services available for the script and provides a set of guidelines within which the script is executed

Create your first script with VB!!
• Open note pad
• Write your script. Exampe: MsgBox "This is my First VB script"
• Save the document in a known location with .vbs extension. For example: MyScript.vbs
• Go to the location where your script is saved and double on it.
• WSH invokes the VB script engine and runs your script. A message box will be displayed with a message "This is my First VB script"

VB Script variables
A variable is a place holder that refers to a Computer memory location where we can store program information that may change during the script run-time.

• VBScript has only one data type called a Variant.
• A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used.
• Because Variant is the only data type in VBScript, it is also the data type returned by all functions in VBScript.
• At its simplest, a Variant can contain either numeric or string information.
• Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of information.
• For example, we can have numeric information that represents a date or a time.
• When used with other date or time data, the result is always expressed as a date or a time.

Declaring Variables
We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement.

Dim Statement:
• Declares variables and allocates storage space.
• Variables declared with Dim at the script level are available to all procedures within the script.
Ex: Dim MyVar

Public Statement:
• Declares public variables and allocates storage space.
• Public statement variables are available to all procedures in all scripts.
Ex: Public MyVar

Private Statement:
• Declares private variables and allocates storage space.
• Private statement variables are available only to the script in which they are declared.
Ex: Private MyVar

We can declare multiple variables by separating each variable name with a comma.
For example:
Dim x, Top, Bottom, Left, Right

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
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:
o Must begin with an alphabetic character.
o Cannot contain an embedded period.
o Must not exceed 255 characters.
o Must be unique in the scope in which it is declared.

Tip: meaningfull prefix to variables to indicate the subtypes i.e
iCounter (integer), strName (String), bFlag (Boolean), dteToday (Date)


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:
Dim A
A = 200

Dim City
City = “Hyderabad”
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(10) = 55

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

AnyVariable = 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 resize a dynamic array, although if we make an array smaller, we lose the data in the eliminated elements.

1 comment: