VBScript Microsoft Visual basic Scripting Edition is the newest member of the Visual Basic family of program development languages, and it applies flexible Script to a wider range of areas, including Microsoft Inter NET Explorer script and Web server script in Microsoft Internet Information Server.
Easy to learn and easy to use
If you already know Visual Basic or Visual Basic for Applications, you'll soon be familiar with VBScript. Even if you have not learned Visual Basic, you can use all of the Visual Basic language to program programming as long as you learn VBScript. Although you can learn about VBscript from several Web pages in this tutorial, this tutorial does not tell you how to program. To learn to program, read the "Step by Step" published by Microsoft Press.
ActiveX Script
VBScript uses Activex™script to talk to the host application. Using ActiveX script, browsers and other host applications no longer require special integration code for each Script part. ActiveX script enables the host to compile Script, get and invoke entry points, and manage the namespaces available to the developer. With ActiveX script, a language vendor can establish a standard Script runtime language. Microsoft will provide run-time support for VBScript. Microsoft is working with multiple Internet groups to define ActiveX script standards so that the script engine can be interchanged. ActiveX Script can be used in microsoft®internet Explorer and microsoft®internet information Server.
VBScript in other applications and browsers
As a developer, you can use the VBScript source implementation program for free in your product. Microsoft provides VBscript binary implementations for 32-bit WINDOWS®API, 16-bit Windows APIs, and macintosh®. VBScript is integrated with the world Wide Web browser. VBScript and ActiveX script can also be used in other applications as normal script languages.
What is a VBScript data type?
VBScript has only one data type, called a Variant. A Variant is a special type of data that, depending on how it is used, can contain different categories of information. Because the Variant is the only data type in VBScript, it is also the data type of the return value of all functions in VBScript.
The simplest Variant can contain numeric or string information. A Variant is used as a numeric processing in a numeric context and is used as a string in the context of a string. That is, if you use data that looks like a number, VBScript assumes it is a number and handles it in a digital way. Similarly, if the data used is only a string, VBScript will be treated as a string. Of course, you can also include a number in quotation marks ("") to make it a string.
Variant Subtype
In addition to simple numbers or strings, a Variant can further differentiate between specific meanings of numeric information. For example, use numeric information to represent a date or time. When this type of data is used with other date or time data, the result is always expressed as a date or time. Of course, numeric information is varied from Boolean values to floating-point numbers. A Variant contains a numeric information type called a subtype. In most cases, you can put the data you want into a variant, and the variant will operate in the way that best applies to the data it contains.
The following table shows the data subtypes that the Variant contains:
Child type description
Empty the uninitialized Variant. For a numeric variable, the value is 0; for a string variable, the value is a zero-length string ("").
Null does not contain a Variant of any valid data.
Boolean contains True or False.
Byte contains an integer between 0 and 255.
The integer contains integers from 32,768 to 32,767.
currency-922,337,203,685,477.5808 to 922,337,203,685,477.5807.
A Long contains integers from 2,147,483,648 to 2,147,483,647.
Single contains single-precision floating-point numbers, with a negative range from -3.402823E38 to -1.401298E-45, and a positive range from 1.401298E-45 to 3.402823E38.
Double contains double-precision floating-point numbers, with a negative range from -1.79769313486232E308 to -4.94065645841247E-324, and a positive range from 4.94065645841247E-324 to 1.79769313486232E308.
Date (time) contains a number that represents a date ranging from January 1, 100 A.D. to December 31, 9999.
String contains a variable-length string with a maximum length of 2 billion characters.
Object contains objects.
The error contains the wrong number.
You can use conversion functions to transform subtypes of data. Alternatively, you can use the VarType function to return the Variant subtype of the data.
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.
The length cannot exceed 255 characters.
Must be unique within the 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.