VBScript Tutorial lesson four VBScript variables

Source: Internet
Author: User
Tags array arrays contains expression variables scalar variable
Vbscript| Variable | tutorials

  What is a variable?

A variable is a handy placeholder for referencing a computer's memory address, which stores program information that can be changed when the Script is run. For example, you can create a variable named Clickcount to store the number of times a user clicks an object on a Web page. Using a variable does not require an understanding of the variable's address in the computer's memory, as long as you can view or change the value of the variable by referencing the variable name. There is only one basic data type in VBScript, a variant, so all variables have a variant of the data type.

  declaring variables

One way to declare a variable is to use the DIM statement, public statement, and Private statement to explicitly declare the variable in the Script. For example:

Dim Degreesfahrenheit

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

Dim top, Bottom, left, right

Another way is to implicitly declare a variable by using the variable name directly in the Script. This is usually not a good habit, because it can sometimes cause unexpected results when the Script is run because the variable name is misspelled. Therefore, it is best to explicitly declare all variables using the Option Explicit statement as the first statement of the Script.

Naming rules

Variable naming must follow VBScript's standard naming conventions. Variable naming must follow:

• The first character must be a letter.

• cannot contain an embedded period.

• Length cannot exceed 255 characters.

• Must be unique within a declared scope.

Scope and lifetime of variables

The scope of a variable is determined by the position in which it is declared. If you declare a variable in a procedure, only the code in the procedure can access or change the value of the variable, at which point the variable has a local scope and is called a procedure-level variable. If you declare a variable outside of a procedure, the variable can be recognized by all procedures in the script, called a script-level variable, with a script-level scope.

The time at which a variable exists is called a survival period. The lifetime of a script-level variable is from the moment it is declared until the script runs out. For a procedure-level variable, its lifetime is only the time that the process is running, and the variable disappears when the process ends. Local variables are ideal temporary storage space when executing a procedure. Local variables of the same name can be used in different procedures, because each local variable is recognized only by the procedure that declares it.

  Assigning values to variables

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

B = 200

  scalar variables and array variables

In most cases, you simply 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 related values to a variable, so you can create a variable that contains a series of values, called an array variable. An array variable and a scalar variable are declared in the same way, except that the variable name is followed by parentheses () when the array variable is declared. The following example declares a one-dimensional array that contains 11 elements:

Dim A (10)

Although the number shown in parentheses is 10, because all arrays in VBScript are based on 0, the array actually contains 11 elements. In a 0-based array, the number of array elements is always the number shown in parentheses plus 1. This array is called an array of fixed sizes.

Use the index to assign values to each element in an array. From 0 to 10, assign the 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 an index to retrieve data for the array element that you want. For example:

. . .

somevariable = A (8)

. . .

Arrays are not limited to one dimension. The maximum number of dimensions for an array can be 60 (although most people cannot understand more than 3 or 4 of the dimensions). When you declare a multidimensional array, use a comma 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 of 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 number represents the number of columns.

You can also declare a dynamic array, which is the size of the array when the Script is run. The initial declaration of an array uses a DIM statement or a ReDim statement. However, for a dynamic array, the parentheses do not contain any numbers. For example:

Dim MyArray ()

ReDim Anotherarray ()

To use a dynamic array, you must then use ReDim to determine the dimensions and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25, and the subsequent REDIM statement resizes the array to 30, while using the Preserve keyword to retain the contents of the array when resized.

ReDim MyArray (25)

. . .

ReDim Preserve MyArray (30)

There is no limit to the number of times the dynamic array is resized, but it should be noted that the size of the array will lose the data of the deleted element.



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.