VBScript tutorial lesson 4 VBScript variables


VBScript variable

Variables are easy-to-use placeholders that reference computer memory addresses that store program information that can be changed when a script runs. For example, you can create a variable called clickcount to store the number of times a user clicks an object on a web page. You don’t need to know the address of the variable in the computer memory to use the variable. You can view or change the value of the variable as long as you reference the variable through the variable name. In VBScript, there is only one basic data type, namely variant, so the data type of all variables is variant.

Declare variable

One way to declare variables is to explicitly declare variables in scripts using dim statements, public statements, and private statements. For example:

Dim DegreesFahrenheit

When declaring multiple variables, use commas to separate variables. For example:

Dim Top, Bottom, Left, Right

Another way is to implicitly declare variables by using variable names directly in the script. This is usually not a good habit because it can sometimes lead to unexpected results when running a script because the variable name is misspelled. Therefore, it is best to use the option explicit statement to explicitly declare all variables as the first statement of the script.

Naming rules

Variable naming must follow the standard naming rules of VBScript. Variable naming must follow:

  • The first character must be a letter.
  • Cannot contain embedded periods.
  • The length cannot exceed 255 characters.
  • Must be unique within the declared scope.

Scope and survival of variables

The scope of a variable is determined by where it is declared. If a variable is declared in a procedure, only the code in the procedure can access or change the value of the variable. At this time, the variable has a local scope and is called a procedure level variable. If a variable is declared outside the procedure, it can be recognized by all procedures in the script. It is called script level variable and has script level scope.

The time the variable exists is called survival. Script level variables live from the moment they are declared until the end of the script. For process level variables, their survival period is only the running time of the process. After the process is completed, the variables disappear. Local variables are ideal temporary storage space when executing procedures. Local variables with the same name can be used in different procedures because each local variable is only recognized by the procedure that declares it.

Assign values to variables

Create an expression in the following form to assign a value to the variable: the variable is on the left of the expression and the value to be assigned is on the right of the expression. For example:

B = 200

Scalar and array variables

In most cases, you only need to assign a value to the declared variable. Variables that contain only one value are called scalar variables. Sometimes it is more convenient to assign multiple related values to a variable, so you can create a variable containing a series of values, called an array variable. Array variables and scalar variables are declared in the same way. The only difference is that when declaring array variables, the variable name is followed by parentheses (). The following example declares a one-dimensional array containing 11 elements:

Dim A(10)

Although the number shown in parentheses is 10, since all arrays in VBScript are based on 0, this array actually contains 11 elements. In a 0-based array, the number of array elements is always the number shown in parentheses plus 1. Such arrays are called fixed size arrays.

An index is used in an array to assign a value to each element of the array. From 0 to 10, assign data to the elements of the array, as follows:

A(0) = 256
A(1) = 324
A(2) = 100
. . .
A(10) = 55

Similarly, the index can be used to retrieve the data of the required array elements. For example:

. . .
SomeVariable = A(8)
. . .

Arrays are not limited to one dimension. The maximum dimension of an array can be 60 (although most people cannot understand a dimension greater than 3 or 4). When declaring multidimensional arrays, use commas to separate each number in parentheses that represents the size of the array. In the following example, the mytable variable is a two-dimensional array with 6 rows and 11 columns:

Dim MyTable(5, 10)

In a two-dimensional array, the first number in parentheses represents the number of rows and the second number represents the number of columns.

You can also declare a dynamic array, that is, an array whose size changes when the script is run. Use a dim statement or a ReDim statement for the initial declaration of the array. However, for dynamic arrays, there are no numbers in parentheses. For example:

Dim MyArray()
ReDim AnotherArray()

To use dynamic arrays, you must then use ReDim to determine the dimension and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25, while the subsequent ReDim statement resizes the array to 30, while using the preserve keyword to preserve the contents of the array when resizing.

ReDim MyArray(25)
. . .
ReDim Preserve MyArray(30)

There is no limit to the number of times to resize the dynamic array. If you adjust the size of the array, you will lose the data of the deleted elements.

Recommended Today

Five years, are you still using junit4?

junit5 JUnit 5 was released in 2017. Are you still using JUnit 4? What is junit5 Unlike previous versions of JUnit, JUnit 5 consists of several different modules of three different subprojects. JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage JUnit PlatformIt provides the basis for starting the test framework on the […]