VB Script (Beginners) (I)

VBScript (short for Visual Basic Scripting Edition) is an Active Scripting language, developed by Microsoft, which uses the Component Object Model to access elements of the environment within which it's running.

The language's syntax reflects its origins as a limited variation of Microsoft's Visual Basic programming language.

Data types

VBScript supports only one data type called ‘Variant’. The variant data type is a special kind of data type that can contain different kinds of information. It is the default data type returned by all functions in VBScript. A variant behaves as a number when it is used in a numeric context and as a string when used in a string context. It is possible to make numbers behave as strings by enclosing them within quotes.

Variables

A variable is a placeholder that refers to a memory location that stores program information that may change at run time. A variable is referred to by its name for accessing the value stored or to modify its value.

Variable Declaration

Variables in VBScript can be declared in three ways:

1. Dim Statement

2. Public Statement

3. Private Statement

1. Dim Statement

This standard VB statement declares variables for use in a Basic program.

Syntax

Dim [Shared] variableName [As[ New] type] [, variableName [As[ New] type]] ...

Usage

Dim is a declaration statement. It is an abbreviation for Declare in Memory; however, you must use the short form.

VariableName must begin with a letter and contain only letters, numbers, and underscores.

A name can also be delimited by brackets, and any character can be used inside the brackets, except for other brackets.

Dim my_1st_variable As String Dim [one long and strange! variable name] As String

Note: Variables declared with Dim at the script level are available to all procedures within the script.

At the procedure level, variables are available only within the procedure.

Public statement variables are available to all procedures in all scripts. Private statement variables are available only to the script in which they are declared.
There are standard rules for naming variables in VBScript.

A variable name:

=>Must begin with an alphabetic character.

=>Cannot contain an embedded period.

=>Must not exceed 255 characters.

=>Must be unique in the scope in which it is declared.

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:

B = 200

Scalar Variables

Much of the time, you only want to assign a single value to a variable you have declared. A variable containing a single value is a scalar variable.

Array Variables

To assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable.

The available data types for arrays are numbers, strings, variants, and records. Arrays of arrays and objects are not supported.

Array variables are declared by including a subscript list as part of the variableName. The syntax to use for variableName is:

Dim variable([[startSubcript To] endSubscript, ...]) As typeName

or

Dim variable_with_suffix([[startSubcript To] endSubscript, ... ])

Numbers

Numeric variables can be declared using the As clause and one of the following numeric types:

currency, integer, long, single, double.

Numeric variables can also be declared by including a type character as a suffix to the name. Numeric variables are initialized to 0.

Objects

Object variables are declared using an As clause and a typeName of a class. Object variables can be set to refer to an object, and then used to access members and methods of the object using dot notation.

Dim COMObject As ObjectSet COMObject = CreateObject("spoly.cpoly")COMObject.reset

An object can be declared as New for some classes.

For example:

Dim variableName As New classNamevariableName.methodName

In such instances, a Set statement is not required to create the object variable; a new object is allocated when the variable is used.

Records

Record variables are declared by using an As clause and a typeName that has been defined previously using the Type statement. The syntax to use is:

Dim variableName As typeName

Records are made up of a collection of data elements called fields. These fields can be of any numeric, string, variant, or previously defined record type.

For details on accessing fields within a record, read Type Statement.

Strings

Siebel VB supports two types of strings:

fixed-length and dynamic.

Fixed-length strings are declared with a specific length (between 1 and 32767) and cannot be changed later.

Use the following syntax to declare a fixed-length string: Dim variableName As String * length

Dynamic strings have no declared length, and can vary in length from 0 to 32,767.

The initial length for a dynamic string is 0.

Use the following syntax to declare a dynamic string:

Dim variableName$

or

Dim variableName As String

When initialized, fixed-length strings are filled with zeros.

Dynamic strings are initialized as zero-length strings.

Variants

Declare variables as variants when the type of the variable is not known at the start of, or might change during, the procedure.

For example, a variant is useful for holding input from a user when valid input can be either text or numbers.

Use the following syntax to declare a variant:

Dim variableName

or

Dim variableName As Variant

Variant variables are initialized to vartype Empty.

No comments:

Post a Comment