VBScript tutorial Lesson 4 VBScript Variables

Source: Internet
Author: User

What is a variable?

A variable is a convenient placeholder used to reference the computer memory address, which can be used to store the address that can be changed during script running.ProgramInformation. For example, you can create a variable named clickcount to store the number of times a user clicks an object on the web page. When using a variable, you do not need to know the address of the variable in computer memory. You only need to reference the variable through the variable name to view or change the value of the variable. In VBScript, there is only one basic data type, namely variant. Therefore, the Data Types of all variables are variant.

Declare Variables

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 multiple variables are declared, use commas to separate the variables. For example:
Dim top, bottom, left, right

Another way is to implicitly declare a variable by directly using the variable name in the script. This is usually not a good habit, because sometimes unexpected results occur when running scripts due to misspelling of variable names. Therefore, it is best to use the option explicit statement to explicitly declare all variables and use them 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 its position. If a variable is declared during the process, onlyCodeYou can access or change the value. A variable has a local scope and is called a process-level variable. If a variable is declared outside the process, the variable can be recognized by all processes in the script. It is called a script-level variable and has a script-level scope.

The time when a variable exists is called the retention period. The script-level variable retention period starts from the declared moment until the script operation ends. For a process-level variable, its survival period is only the time when the process runs. After the process ends, the variable disappears. Local variables are ideal for temporary storage during execution. You can use local variables with the same name in different processes, because each local variable is identified only by the declared process.

Assign values to variables

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

B = 200

Scalar variables and array Variables

In most cases, you only need to assign a value to the declared variable. A variable that contains only one value is called a scalar variable. Sometimes it is more convenient to assign multiple values to a variable. Therefore, you can create a variable that contains 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 an array variable is declared, the variable name is enclosed by parentheses (). The following example declares a one-dimensional array containing 11 elements:

Dim A (10)

Although the number shown in parentheses is 10, because all arrays in VBScript are based on 0, this array actually contains 11 elements. In an Array Based on 0, the number of array elements is always the number displayed in parentheses plus 1. These arrays are called fixed-size arrays.

Use indexes in the array to assign values 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, you can use indexes 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 the array can be 60 (although most people cannot understand the dimension that exceeds 3 or 4 ). When declaring a multi-dimensional array, use commas to separate each number in the brackets that represents the array size. 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 digit in parentheses represents the number of rows, and the second digit represents the number of columns.
You can also declare a dynamic array, that is, the array that changes when the script is run. The dim statement or redim statement is used for the initial declaration of the array. But for dynamic arrays, parentheses do not contain any numbers. For example:

Dim myarray ()
Redim anotherarray ()

To use a dynamic array, you must 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 re-adjusts the size of the array to 30, at the same time, use the preserve keyword to retune the size of the array to retain the content.

Redim myarray (25)
...
Redim preserve myarray (30)

There is no limit on the number of times the dynamic array size is re-adjusted, but note: adjust the size of the array by hour, the data of the deleted element will be lost.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.