Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The tutorial of VBScript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the tutorial of VBScript". In the daily operation, I believe that many people have doubts about the tutorial of VBScript. The editor has consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "tutorial of VBScript"! Next, please follow the editor to study!

Microsoft Visual Basic Scripting Edition is the newest member of the Visual Basic family of programming languages. It applies flexible Script to a wider range of fields, including Web client Script in Microsoft Internet Explorer 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 will quickly become familiar with VBScript. Even if you haven't learned Visual Basic, as long as you learn VBScript, you will be able to program in all Visual Basic languages. Although you can learn VBscript from several Web pages of this tutorial, this tutorial does not tell you how to program. To learn about 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 hosting 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 developers. With ActiveX Script, language vendors can build standard Script runtime languages. Microsoft will provide runtime support for VBScript. Microsoft is working with multiple Internet groups to define ActiveX Script standards to make Script engines interchangeable. 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 VBScript source implementations in your products for free. Microsoft provides a binary implementation of VBscript for 32-bit Windows ®API, 16-bit Windows API, and Macintosh ®. VBScript integrates with World Wide Web browsers. VBScript and ActiveX Script can also be used as normal 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 that can contain different categories of information depending on how it is used. Because 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. Variant is treated as a number when used in a numeric context and as a string when used in a string context. That is, if you use data that looks like a number, VBScript assumes that it is a number and processes it in a manner that applies to numbers. Similarly, if the data used can only be a string, VBScript will handle it as a string. Of course, you can also enclose a number in quotes ("") to make it a string.

Variant subtype

In addition to simple numbers or strings, Variant can further distinguish the specific meaning of numeric information. For example, numerical information is used to represent a date or time. When such data is used with other date or time data, the result is always represented as a date or time. Of course, numerical information is varied from Boolean values to floating-point numbers. The type of numeric information contained in Variant is called a subtype. In most cases, you can put the required data into Variant, and Variant operates in the manner that is most appropriate for the data it contains.

The following table shows the data subtypes that Variant contains:

Subtype description

Empty uninitialized Variant. For numeric variables, the value is 0; for string variables, the value is a zero-length string ("").

Null does not contain any Variant of valid data.

Boolean contains either True or False.

Byte contains integers between 0 and 255.

Integer contains integers between-32768 and 32767.

Currency-922337203685477.5808 to 922337203685477.5807.

Long contains integers between-2147483648 and 2147483647.

Single contains single-precision floating-point numbers, ranging from-3.402823e+26 trillion to-1.401298E-45 for negative numbers and from 1.401298E-45 to 3.402823e+26 trillion for positive numbers.

Double contains double-precision floating-point numbers, with negative numbers ranging from-1.79769313486232E308 to-4.94065645841247E-324 and positive numbers from 4.94065645841247E-324 to 1.79769313486232E308.

Date (Time) contains numbers that represent dates, ranging from January 1, 100 to December 31, 9999.

String contains variable-length strings with a maximum length of 2 billion characters.

Object contains objects.

Error contains the error number.

You can use conversion functions to convert subtypes of data. In addition, you can use the VarType function to return the Variant subtype of the data.

What is a variable?

A variable is an easy-to-use placeholder that refers to the computer's memory address, which stores program information that can be changed while Script is running. 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 computer memory to use the variable. You can view or change the value of the variable simply by referencing it through its name. There is only one basic data type in VBScript, Variant, so the data type of all variables is Variant.

Declare variable

One way to declare variables is to explicitly declare variables in Script using Dim statements, Public statements, and Private statements. For example:

Dim DegreesFahrenheit

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

Dim Top, Bottom, Left, Right

Another way is to implicitly declare variables by using variable names directly in Script. This is usually not a good habit, as it can sometimes lead to unexpected results when running Script due to misspelled variable names. 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 conventions 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.

The scope and survival period of variables

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

The time for 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 end of the Script run. For a process-level variable, its lifetime is only the time the process is running, and when the process ends, the variable disappears. Local variables are ideal temporary storage space when executing a procedure. Local variables with the same name can be used in different procedures because each local variable is identified only by the process that declares it.

Assign a value to a variable

Create an expression in the following form to assign a value to a 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. Variables that contain only one value are called scalar variables. Sometimes it is more convenient to assign multiple related values to a single variable, so 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, except that the variable name is followed by parentheses () when declaring array variables. The following example declares an one-dimensional array of 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 a 0-based array, the number of array elements is always the number shown in parentheses plus 1. Such an array is called a fixed-size array.

Use an index in an 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, the data of the desired array elements can be retrieved using the index. 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 a multidimensional array, separate each number in parentheses that represents the size of the array with a comma. 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 dynamic arrays, that is, arrays that change in size when you run Script. Use Dim or ReDim statements for the initial declaration of the array. For dynamic arrays, however, 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 retain the contents of the array when resized.

ReDim MyArray (25)

. . .

ReDim Preserve MyArray (30)

There is no limit to the number of times you can resize a dynamic array, but it should be noted that resizing the array will lose the data of the deleted element.

Undefined

At this point, the study of "VBScript tutorial" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report