Variables
- Updated2024-09-12
- 5 minute(s) read
VBScript Language Directory > Variables
Variables
A variable identifies a position in the computer memory where you can store program data. You can use the variable name to call or to change the value of this position.
Declaring Variables
Before you use a variable in a script, use the statements Dim, Public, or Private to declare a variable explicitly and to provide memory space:
Dim Position
If you want to declare several variables, separate the variables with commas:
Dim Top, Bottom, Right, Left
In VBS you can use undeclared variables by including the variable names anywhere in the script. As soon as VBS finds an undeclared variable, it creates storage space for the variable. The disadvantage is that VBS does not recognize incorrect variable names, which can lead to unexpected results when the script is executed. To prevent this program behavior use the Option Explicit statement, which forces the explicit declaration of all variables. The Option Explicit statement should always be the first statement in the script.
![]() | Note Within a script, you can access the VBS variables you have defined, as well as all variables that DIAdem provides. Unlike VBS variables, DIAdem variables have a specific data type and thus value range. |
Naming Conventions
The following rules apply for variable names:
- Variable names must start with a letter of the alphabet. You can also use numbers and the underscore _ in the name. All other special characters are invalid.
- Variable names must not exceed 255 characters.
- Variable names must be unique within the area of validity that the names were declared.
![]() | Note If the name of a VBS variable is the same as the name of a DIAdem variable, the VBS variable replaces the DIAdem variable. You must then use Diadem.Variable to call the original DIAdem variable. |
Scope and Lifetime of Variables
The scope of a variable depends on where you declare the variable. If you declare a variable in a procedure, only the code in this procedure can access or change the variable value. This variable has a local scope and is called a Variable on procedure level. If you declare a variable outside a procedure, it is a variable on script level. This variable is valid for all procedures in a script. A variable on script level is valid from the time of declaration to the end of the script. A variable on procedure level exists only as long as the procedure is active. The variable is destroyed when you exit the procedure. Local variables are suitable as a temporary memory while the procedure is running. You can use local variables with the same name in several procedures, because each variable is only recognized by the procedure in which it was declared.
Assigning Values
To assign values to variables, create an expression according to the following scheme. The variable is on the left side of the expression and the value to be assigned to the variable is on the right side:
Position = 200
Empty
If you declare a variable but do not assign a value to the variable, the variable contains the value Empty. Empty is 0 for numerical variables and an empty string "" for text variables. Use the IsEmpty method to check the initialization of a variable.
Null
If the variable contains the value Null, the variable contains no valid data. Whereas Empty indicates that a variable was not assigned a value, a variable can only contain the value Null if the value Null was explicitly assigned to the variable. Use the IsNull method to check whether a variable value is valid. If you use valid data and invalid data (Null) in operations, the result is always an invalid value (Null).
Scalar Variables and Array Variables
As a rule, you only assign single values to variables. These variables are called Scalar variables. You can also define variables that accept a sequence of connected values. These variables are called array variables. Define array variables in the same way as scalar variables but set parentheses after the variable name for the declaration. In the parentheses you specify the size of the array:
Dim MyArray(10)
This example declares a one-dimensional array with 11 elements. Although the value 10 is given in parentheses, this array contains 11 elements because all arrays in VBS are zero-based. This means all arrays begin with the index 0. The number of elements in a zero-based array is always greater by one than the value specified in the parentheses. Such an array is called a fixed size array. You can assign exactly one value to each element of the array. Use an index to assign data to the individual elements. With the indexes 0 to 10 you assign values to the elements as follows:
MyArray(0) = 2 MyArray(1) = 23 MyArray(2) = 234 ... MyArray(10) = 2345
You call the value of any element of the array in a similar way:
MyVariable = MyArray(3)
Arrays are not limited to one dimension. They can include up to 60 dimensions. Separate the array sizes between the parentheses with commas to declare several dimensions:
Separate commas:
Dim MyTable(5,10)
The MyTable variable is a two-dimensional array with 6 rows and 11 columns. In a two-dimensional array, the first number always specifies the number of rows and the second number specifies the number of columns. You can also declare arrays and change the array size while the script is running. These arrays are called dynamic arrays. Declare the array at the beginning with a Dim statement as you do with all other arrays. The difference is that you do not specify a size or a number of dimensions inside the parentheses.
Dim MyArray()
To use a dynamic array, specify the number and the value of the dimensions with ReDim:
ReDim MyArray(10) ... ReDim Preserve MyArray(15)
These statements use ReDim to specify the size of the dynamic array as 10. The following ReDim statement changes the size of MyArray to 15. The keyword Preserve saves the present content of the array, that means the content of the first eleven elements are maintained. If the keyword is missing, the contents of the array to be resized are completely deleted. You can change the size of a dynamic array any number of times. However, the data of the eliminated elements is lost when the array is reduced. Notice also, that you can modify only the last dimension of the array with the ReDim statement.
Related Topics
Array | Dim | Private | Public | ReDim | IsArray | Erase | LBound | UBound
