Introduction to VBScript

Source: Internet
Author: User
Tags ranges

Microsoft Visual Basic Scripting Edition is the latest member of the Visual Basic family of programming languages. It applies flexible scripts to a wider range of fields, includes the Web Client Script in Microsoft Internet Explorer and the Web Server Script in Microsoft Internet Information Server.

Easy to learn and use

If you already know Visual Basic or Visual Basic for Applications, you will soon be familiar with VBScript. Even if you have never learned Visual Basic, you can use all the Visual Basic languages for programming as long as you learn VBScript. Although you can learn VBscript from several Web pages of this tutorial, this tutorial does not show you how to program. To learn programming, read Step by Step published by Microsoft Press.

ActiveX Script

VBScript uses ActiveX Script to talk to the Host application. With ActiveX Script, browsers and other host applications no longer need special integration code for each Script part. ActiveX Script allows the host to compile scripts, obtain and call endpoints, and manage namespaces available to developers. Through ActiveX Script, language vendors can establish a standard Script runtime language. Microsoft will provide support for running VBScript. Microsoft is defining ActiveX Script standards with multiple Internet groups to make the Script Engine interchangeable. ActiveX Script is available in Microsoft Internet Explorer and Microsoft Internet Information Server.

VBScript in other applications and browsers

As a developer, you can use VBScript source implementation programs for free in your products. Microsoft provides VBscript binary implementation programs for 32-bit Windows APIs, 16-bit Windows APIs, and Macintosh. VBScript is integrated with the World Wide Web browser. VBScript and ActiveX Script can also be used as common Script languages in other applications.

What is the VBScript data type?
VBScript has only one data type called Variant. Variant is a special data type. It can contain different types of information according to the method used. Because Variant is the only data type in VBScript, it is also the data type returned by all functions in VBScript.

The simplest Variant can contain numbers or strings. Variant is used as a number in the numeric context and as a string in the string context. That is to say, if you use data that looks like a number, VBScript assumes it is a number and processes it in a numeric way. Similarly, if the data used can only be a string, VBScript will process it by string. Of course, you can also include a number in quotation marks ("") to make it a string.

Variant subtype

In addition to a simple number or string, Variant can further differentiate the specific meaning of the value information. For example, a value is used to indicate a date or time. When such data is used together with other date or time data, the result is always expressed as a date or time. Of course, from a Boolean value to a floating point number, the numerical value information is diverse. The numeric information type contained in Variant is called a subtype. In most cases, you can put the required data into Variant, and Variant will perform operations in the way that best applies to the data it contains.

The following table lists the data subtypes contained in Variant:

Child type description
Empty Does not initialize Variant. For numeric variables, the value is 0; for string variables, the value is a string of zero length ("").

Null does not contain any valid data Variant.

Boolean contains True or False.

Byte contains an integer between 0 and 255.

An Integer contains an Integer between-32,768 and 32,767.

Currency-922,337,203,685,477.5808 to 922,337,203,685,477.5807.

Long contains an integer between-2,147,483,648 and 2,147,483,647.

Single contains a Single-precision floating point number. The negative number ranges from-3.402823E38 to-1.401298E-45, and the positive number ranges from 1.401298E-45 to 3.402823E38.

Double contains Double-precision floating point numbers. The negative value ranges from-1.79769313486232E308 to-4.94065645841247E-324. The positive value ranges from 4.94065645841247E-324 to 1.79769313486232e.

Date (Time) contains the number representing the Date, which ranges from January 1, January 1, 100 AD to January 1, December 31, 9999 AD.

String contains a variable-length String. The maximum length can be 2 billion characters.

Object contains objects.

Error contains the Error code.

You can use the Conversion Function to convert the subtypes of data. In addition, you can use the VarType function to return the Variant sub-type of data.

What is a variable?

A variable is a convenient placeholder used to reference the computer memory address, which can store information about programs that can be changed during Script running. 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.
It cannot exceed 255 characters.
It 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, only the code in the process can access or change the value. At this time, the 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.