VBScript language base summary

xiaoxiao2021-04-09  521

VBScript language base summary

In a few previous articles, we have introduced the most basic knowledge of the VBScript language to the reader through a series of runoffed instances. In this way, the language framework is exempt from omissions. In this article, we will make a small linguistic element and make a summary of the basic tutorial of the VBScript language, and the entry is over. Further study, please see my - "Study of VBScript and JavaScript Language Practical Technology" (Advanced).

A VBScript data type VBScript has only one data type, called

Variant.

Variant is a special data type, depending on the way, it can contain different categories of information. because

Variant is the only data type in VBScript, so it is also the data type of all functions of all functions in VBScript. but,

Variant contains various subtypes, the following table shows

Variant contains subtype:

Sub Type Description EMPTY is not initialized Variant. For numerical variables, the value is 0; for the string variable, the value is zero length string (""). NULL does not contain any valid data of Variant. Boolean contains True or False. BYTE contains an integer between 0 and 255. Integer includes an integer between -32, 768 to 32, 767. Currency-922, 337, 203, 685, 477.5808 to 922, 337, 203, 685, 477.5807. LONG includes an integer between -2, 147, 483, 648 to 2, 147, 483, 647. Single includes single-precision floating point, negative range range from -3.402823E38 to -1.401298E-45, positive range ranges from 1.401298E-45 to 3.402823E38. Double contains double precision floating point, negative range from -1.79769313486232E308 to -4.94065645841247E-324, positive range from 4.9477645841247E-324 to 1.79769313486232E308. Date (TIME) contains numbers representing the date, date range from January 1, AD to December 31, 9999. String contains a growing string with a maximum length of 2 billion characters. Object contains an object. Error contains an error number. You can use the conversion function to convert the subtype of the data. In addition, you can use

VARTYPE function returns data

Variant subtype.

Two constants and variables

What is a constant? The constant is a name having a certain meaning for replacing a number or string, and its value does not change. VBScript defines a lot of inherent constants. For more information, see the VBScript language reference. You can create user-defined constants in VBScript using a const statement. Use the const statement to create a name or numeric constant with a certain meaning of a sense, and give them the original value. E.g:

Const mystring = "This is a string."

Const myage = 49

What is a variable? Variables are actually a memory address that gives the name (variable name), as long as the variable is referenced by the variable name, can be viewed or changed. In VBScript because there is only one basic data type, all variables have a variant. In VBScript can also use variables without declaration, but the first declaration is a good habit, let alone variables in other languages ​​must first declare the reference. One way to declare variables is to explicitly declare variables in the script using a DIM statement, a public statement, and a private statement. Named the variable must follow the standard naming rules for VBScript. Variable naming must follow: The first character must be a letter. You cannot include embedded stations. The length cannot exceed 255 characters. There must be unique in the scope of the declared domain.

The field of variables and the survival period

The scope of the variable is determined by the declaration of its position. If the variable is declared during the process, only the code in the process can access or change the variable value, and the variable has a local scope and is called

Process grade variable. If the variable is declared outside the process, the variable can be identified by all the processes in the script, called

Script-level variable with scripting level scope.

The time of the variable is called the survival period. The survival of the Script-level variable is from the moment of declaration until the script is running. For process-grade variables, the survival period is only the time of the process, and the variable disappears after the process. At the time of execution, local variables are ideal temporary storage space. Local variables of the same name can be used in different processes because each local variable is only declarested its process identification.

Three operators and expressions

VBScript has a complete operator, including arithmetic operators, comparison operators, connection operators, and logical operators. The formula formed by the operator and constant variable is referred to as an expression. Any expression has a final result after the operation, the subtype of this result is considered to be the type of expression.

All operators are listed below.

Modynamic operator , -, *, /, / (whole), mod (more), ^ (seeking power)

Connection operator & (connection string)

Assignment operator = (or assignment statement)

Comparison operator>, <, =,> =, <=, <>

Logical operator NOT (non), AND (with), OR (or), xor (videol), IMP (implication), EQV (equivalent)

In the logical operation, or, or, these three must be mastered, the last three are not commonly used, can not be mastered. For more information, see the VBScript language reference.

The operator priority When the expression contains multiple operators, each part is calculated in a predetermined order, and this order is called an operator priority. It can be used to calculate certain parts of the expression by using parentheses. When calculating, the operator in parentheses is always performed, and then the operator outside the parentheses is performed. However, standard operator priority is followed in parentheses.

The priority of the operator is shown below (high >>):

Arithmetic Operator >> Compare Operators >> Logic Operators.

The comparison operator is all levels, and the logical operator priority is: NOT >> AND >> OR. The priority of the arithmetic operator is self-evident.

Four statement

The statement is the basic element of any programming language, and the program consists of a series of statements. In the previous articles we used the vast majority of basic statements. The basic statement and its grammatical function are listed below to facilitate query.

Statement Name Syntax Form Function Description Call

[Call] Name [argumentlist]

Pass control to the SUB or FUNCTION process. Call verbs can be omitted.

Const

Const constname = expression declares that the constant used to replace the text value.

DIM

DIM VARNAME [([Subscripts])],.

Declare variables and allocate storage space

DO ... loop

Do [{While | Until} Condition] [Statements] [STATEMENTS] LOOP

While: Repeats the sentence block when the condition is True. Until: Repeats the sentence block before the condition becomes True.

Erase

ERASE ARRAY

Reinitialize the elements of the fixed size array and release the storage space of the dynamic array.

EXECUTE

Execute Statements

Perform one or more specified statements.

Exit

EXIT DO [for | function | Property | SUB]

Exit DO ... LOOP, For ... Next, FUNCTION or SUB code blocks.

For ... next

For counter = start to end [step step] [statements] Next

Repeat a group of statements with the number of specified times.

For Each ... next ... Next

For Each Element in Group [Statements] Next

Repeat a group of statements for each element in array or collection.

FUNCTION

Function name [(arglist)] ... END FUNCTION

Declare the name, parameters, and code constituting its main body of the Function process.

IF ... then ... Else

IF Condition Then Statements Else Statements End IF

A set of statements are performed based on the value of the expression.

On Error

ON Error ResMe next

Start the error handler.

Option expedition

Option expedition

Enforce all variables in the script. This statement must appear before any other statement of the script.

Private

Private varname [([Subscripts])] [, ...].

Define private variables and allocate storage space. Define private variables in the Class block.

Public

Public Varname [([Subscripts])] [, ...]

Define public variables and allocate storage space. Define private variables in the Class block.

Randomize

Randomize [Number]

Initialize the random number generator.

Redim

Redim varname (subscripts) [,..

Declaring dynamic array variables in the process grade and assign or reassign the storage space.

REM

REM Comment or 'Comment

Interpretative annotations in the program.

Select Case

Select Case Testexpression Case ExpressionList-1 Statements-1.. [Case Else ExpressionList-N [ElseStatements-N] End Select

One of the groups of statements is performed based on the value of the expression.

Set

Set ObjectVar = ObjectExpression Set Object.EventName = GetRef (procname)

Assign an object to a variable or attribute, or use an event to associate a process reference.

Sub

Sub Name [(arglist)] ... End Sub

Declare the name, parameter, and code constituting its main body.

While ... Wend

While conputing [statements] Wend

When the specified condition is TRUE, a series of statements are performed. To familiarize yourself with the grammar, it is recommended to use the Do Loop statement.

WITH

WITH Object Statements End With When performing a series of statements for an object, the name is omitted inside the AND end with the object name.

In addition to the two cycle of Do Loop and While, other statements are used in front of the previous articles. The following is an example given by Microsoft, how to use the DO ... LOOP statement:

DO Until Defreesp = VBNO

MyNUM = INT (6 * RND 1) 'Generates a random number between 1 and 6.

DEFRESP = MSGBOX (MyNum & "Do you want another number?", Vbeyesno

Loop

DIM CHECK, Counter

Check = true: counter = 0 'initialization variable.

Do 'outer loop.

Do While Counter <20 'Inner Layer Circulation.

Counter = Counter 1 'Add counter.

If counter = 10 Then 'If the condition is True ...

Check = false 'Set the flag value to false.

EXIT DO 'terminates the inner layer cycle.

END IF

Loop

LOOP Until Check = FALSE 'Terminate the outer loop immediately.

Where Vbyesno is a prefabricated constant, value = 4, indicating that the display is the buttons. VBNO = 7 is used to receive the return value of the message box function, indicating whether the button is clicked.

Five functions and subroutines

Functions and subroutines are a process. There are function and sub statements, respectively. Their differences are:

The function returns a unique value and the subroutine does not return the value. When the function is called, it must be added parentheses even if there is no parameters. The calling subroutine cannot be followed by, plus parentheses, and the parameter table table directly follow. The function generally like the variable appears in the expression, and the subcritical shape appears separately.

VBScript predefines many built-in functions and can be used directly. We have described numerical functions, string functions, and date functions in front. Only the type conversion function is added below. General format CXXXX (expression).

Function name return type

CBOOL Boolean

Cbyte byte CCUR

currency

Cdate

date

CDBL

Double precision

Cint

Integer

CLNG

Long integer

CSNG

Single-precision

CSTR

String

In addition, there is a Filter function. Its function is to filter a string array and return an array that meets the conditions. grammar:

FILTER (InputStrings,

Value [

,

Include [

,

Compare]]]

)

The syntax of the FILTER function has the following parameters:

Parameter Description INPUTSTRINGS must. One-dimensional array, to search for strings in it. Value must be selected. String to search. INCLUDE is optional. Boolean value, specify whether the returned sub-string contains Value. If INCLUDE is True, Filter will return an array subset that contains the substring value. If INCLUDE is False, Filter will return an array subset that does not contain sub-string value VALUE. Compare is optional. The digital value indicates the type of comparison string used. Please refer to the value setting section.

Finally, as the end of this article, we give a useful example program. In the application of the database, the most basic job is query, and the result of the query gets a table, which is existing in the form of a two-dimensional array. Our instance is to generate a table with a program and display the query results on the screen.

The target task automatically generates a table whose rantrum is determined by an array as a parameter. Code

Sub TableGenalete (ArrayName) DIM Width1, Row, Colrow = Ubound (ArrayName, 1) Col = Ubound (ArrayName, 2) Width1 = CSTR (INT) & "%" Document.write (Table _ ("100% "," 1 ")) Document.write (" ") for j = 0 to col-1 TDMASS = TD_ (Width1," # c0c0 ") Week = weekdayName (WeekDay (J 1)) TDMASS = Replace (TDMAss, "AA", Week Document.write TdmassNextDocument.write ("") for i = 0 to row-1Document.write ("") for j = 0 to col-1 TDMASS = TD_ (Width1, "# DDDDDD") TDMASS = Replace (TDMAss, "AA", ArrayName (i, j)) Document.write Tdmass Next Document.write ("") NextDocument.write (" ") End Sub

Code comment

The TableGenalete subroutine will generate a table on the page. The first single cycle generates a header line, in order to display, the line content is "Sunday" to "Saturday". The next two cycle enables each cell of the table and its content-array arrayname corresponding to each of the contents. The HTML code generated by the custom function TD_ (), which sets two parameters - widths and background colors. That is, the two parameters are variable, and others are default.

In order to demonstrate the viewing effect, it is also necessary to call it code as follows.

DIM MyArray (5, 7) for i = 0 to 4 for j = 0 to 6 myarray (i, j) = cstr (10 * i j 1) NextNext TableGenalete MyArray

The two custom functions are as follows:

Function TD_ (Width1, Bgcolor1) DIM SSSS = " AA " SS = Replace (SS, "50%", width1) if bgcolor1 <> "" "= replace (s," #fffffff ", bgcolor1) end iftd_ = ssend functionFunction Table_ (Width1, Border1 ) DIM SSSS = "

" ss = replace (SS, "100%", width1) ss = replace The function of the SS, "1", BORDER1) TABLE_ = SSEND FUNCTION function is to establish a string required to write HTML code, where AA replaces it by the corresponding array element when calling. If you don't replace, it doesn't hinder the operation of the program. This function can also simplify, put the AA and the part of the part in the main function, and save the use of Replace, try it yourself.

Run code

Wen Dongfang

2002-12-27

转载请注明原文地址:https://www.9cbs.com/read-133198.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.058, SQL: 9