Variables:



Variable is a placeholder that refers to a memory location that stores program information that may change at run time.

          Declaration:

        Variables are explicitly declared in the script using:

          Dim Statement
          Public statement
          Private statement

        Multiple variables can be declared by separating each variable name with a comma.

        Examples

    Dim intPassngrCnt   'Declaring variable for storing Passenger Count
          Dim MyVar, MyNum   ' Declare two variables

          Naming Convention:

        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

Option Explicit Statement:

*      Declaring Variables is Optional in VB Script
*      Declaring Variables is Optional in VBScript.
*      But, that is not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run.
*      For that reason, the Option Explicit statement is available to require explicit declaration of all variables.
*      Forces explicit declaration of all variables in a script.
*      If used, the Option Explicit statement must appear in a script before any other statements.
*      When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an undeclared variable name, an error occurs.

Variable Scopes and Life Time:

*      A variable's scope is determined by where you declare it.
*      When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable.
*      If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script.
*      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.
*      You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.

VB Script Variables:

Dim Statement:

Description:

*      Declares variables and allocates storage space.
*      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.
*      You can also use the Dim statement with empty parentheses to declare a dynamic array.

Syntax:

          Dim Var1, Var2, etc…

Example:

          Dim strEmpName, intEmpId

Note:

*      When you use the Dim statement in a procedure, you generally put the Dim statement at the beginning of the procedure. 

Private Statement:

Description:

*      Declares private variables and allocates storage space.
*      Private statement variables are available only to the script in which they are declared.

Syntax:

          Private Var1, Var2, etc…

Example:

          Private strEmpName, intEmpId

Public Statement:

Description:

*      Declares public variables and allocates storage space.
*      Public statement variables are available to all procedures in all scripts.

Syntax:

          Public Var1, Var2, etc…

Example:

          Public strEmpName, intEmpId

Assigning Values to Variables:

Description:

*      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

Syntax:

          VariableName = Variable Value

Example:

          intEmpId = 12345678

Scalar Variables:

*      Many 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.

Example:

          Dim strEmpName, intEmpId

Array Variables:

*      Some times you may require assigning 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.
*      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.
*      An Array is a contiguous area in the memory referred to by a common name.
*      Usage:

*      An array is made up of two parts, the array name and the array subscript.
*      The subscript indicates the highest index value for the elements within the array.
*      Each element of an array has a unique identifying index number by which it can be referenced.
*      VBScript creates zero based arrays where the first element of the array has an index value of zero. 

No comments:

Post a Comment