VB Script Variables

Create a Variable with 'Dim' in a procedure, Function or Property statement.

When assigning a value to a variable, strings are enclosed in quotes (") Dates and times are enclosing in number signs (#)

Examples: 
   
Dim StrMyTextString
   
Dim IntMyInteger

Dim MyArray(10)

Then assign values:

IntMyInteger = 200
StrMyTextString = "This is an example string"

To declare variables explicitly, place the Option Explicit statement as the first statement in your script.
Once this is in place all variables must be defined before being used, and attempting to use an undefined variable will raise an error.
This means that a typo in a variable name will immediately raise an error rather than being silently ignored.

Although you can choose a variable name to indicate the data type, all VBScript variables are type: variant.

Public variables

Defined at the Script-level a public Variable is available throughout the entire script. This includes all functions and subroutines.

Syntax:
       Public variableName[([subscripts])][, variableName[([subscripts])]] ...

Key:
    subscripts    The dimensions of an array variable; up to 60 multiple dimensions may be declared.
                  upper [, upper] ... Where upper is the upper bound of the array, The lower bound of an array is always zero. 

Examples: 
   
Public StrMyVariantString, IntMyVariantInteger, anotherVariable
   
Public MyArray(10)

Then assign values:

MyArray(2,4) = 25
StrMyVariantString = "This is an example string"

Private variables

Defined at the Script-level, a Private variable is available only to the script in which they are declared.
They are not inherited by any functions or subroutines.

Syntax:
       Private variableName[([subscripts])][, variableName[([subscripts])]] . . .

Key:
    subscripts    The dimensions of an array variable; up to 60 multiple dimensions may be declared.
                  upper [, upper] ... Where upper is the upper bound of the array, The lower bound of an array is always zero.

Examples: 
   
Private StrMyVariantString, IntMyVariantInteger, anotherVariable

Private MyArray(10)

Constants - the Const statement.

   Constants are fixed values which once defined, cannot be changed.

   For example: 
   
   Const MY_STRING = "This is my string."
   Const MY_PERCENTAGE = 75
   Const DATE_OF_BIRTH = #6-1-97#
   
By convention the Constant name is always in UPPER CASE.

“Whether you think you can or think you can't — you are right” ~ Henry Ford

Related

Array Variables - VBScript array variables.
Set variable = object - Assign an object reference


Copyright © SS64.com 1999-2019
Some rights reserved