VBScript basis, variables, functions, processes,

1 VBScript basis

1.1 Option Explicit

The Option Explicitstatement at the beginning of your script, before any other statements. This is told VBScript, the code requires that all variables must first before using 显式地声明. Now VBScript will no longer allow you to do a statement in the code on the introduction of a new variable
if used Option Explicit, but did not declare the variable VBScriptwill be terminated and prompted with the following error: Variable is undefined: 'lgnSecond'. This is good news, because now you know you want to fix the error. As long as the use of the Option Explicitstatement, VBScriptit will capture the variables of input errors

2 functions and procedures

2.1 Process

As the process is named block Subkeyword recognition. SubIt is subprocedurethe abbreviation, which is "the p-
rocedure" another way of saying.
Using the following syntax of the process:

[Public|Private] Sub Name ([Argument1],[ArgumentN])
[ code inside the procedure ]
End Sub

2.2 Functions

The syntax of the function with the process is the same, the only difference is that keywords Subinstead Function.

[Public|Private] Function Name ([Argument1],[ArgumentN])
[ code inside the function ]
End Function

2.3 call procedures and functions

2.3.1 Procedure Call

Here Insert Picture Description

  • These two legitimate way are equivalent in function, choice of which depends on their preferences.
  • Some people say that the second approach (using Callkeywords) clearer, but it was annoying to play more words.
  • Some people prefer the second way is because it is more like VBScriptthe older generation BASICprogramming language.
  • Two very common way, Visual Basic and VBScript programmers are accustomed to both of these.
  • When you call the process (is a function of relative), if you choose not to use Callkeywords, it can not be passed to the procedure 参数放在圆括号in.
  • Similarly, if you want to use Callkeyword, in parentheses it is indispensable. This method is to use

2.3.2 Function Call

Here Insert Picture Description

2.4 Exit procedure or function

  • Nature will quit after the last line of code in a procedure or function runs out.
  • Sometimes you need an immediate end to a process rather than waiting until all the code run to completion.
  • You can use Exit Substatements (process), you can also use the Exit Functionstatement (function).
  • As long as there Exitstatement, the flow of the code is returned to the caller.

3 variable scope and declarations

3.1 Variable Scope

There are three variable scope in VBScript:

  • 脚本级(script-level)作用域: Variable in the code for the entire script file is valid. In the script file (such as Windows Script Host .VBS file or service activities page .ASP files) - the main scope of the variable is declared ‖ entire script.
  • 过程级(procedure-level)作用域: Variable effective procedure or function. Other code outside of the process, even with a script file code can not access procedure-level variables. Process-level scope is also known as - ‖ local scope, programmers will typically be declared as process-level variable called - local variables ‖.
  • 类级(class-level)作用域: This is a special structure comprising a logical grouping of attributes and methods. In VBScript, a script using the Class...End Classblock of code defines the class. With the main class defined Privatescope of a variable declaration statement is the class level. Other code means that the class can access the variable, but the code outside the class definition, even the same script file code can not access this variable.

3.2 Variable declaration

There are three statements can be used to declare variables: Dim, Privateand Public.

  • Dim: This statement is used to declare a variable script or process level scope. All were declared as script-level variables are automatically valid in the entire script file, either using a Dim, Private or Public. 要声明过程内部的变量(也就是局部变量),必须使用Dim. 不能在过程内部使用 Public 和Private. If a class-level variable Dim the effect is exactly the same with the Public.
  • Private: You can use a script or class-level scope Privatestatement, but can not be used in a procedure or function. If a script-level variable, its role with Dim and Public are identical. All are automatically valid throughout the script file in script-level variable declarations, regardless of Dim, Private or Public. Although VBScript do not need it, but there are still many programmers like to use the Private statement to declare a script-level variable, while the Dim procedure or function reserved for local variables. To declare a private class-level variable, you must use Private. All at the class level using Dim or Public variables declared in the whole class is a valid public property.
  • Public: Public statement may use variable declarations script-level scope, but in effect it is with the Dim or Private is the same. Public 真正有意义的地方就在于类. With class-level variable is declared Public Public properties of this class. Public does not make sense in the script level reasons (script component (script component)) is that variable outside of a script file its existence is of no use. So,Public 唯一有意义的就是创建类的公共属性 . But we know that many programmers do not encourage the use of VBScript Public variables in the class, and prefer to use Private class-level variables and Property Let, Setand Geta combination of processes.
    It contains a lot in these three rules

4 Process Control

4.1 branch structure

4.1.1 if the branch

If...End IfStructure can be very simple, it can become more complicated. Its most simple form of syntax is as follows.

If < expression > Then
< other code goes here >
End If

< expression >You can use any result Trueor Falsestatements (ie Boolean expressions). Can be a mathematical equation
below is a series of different expression evaluation ElseIfblock

If boolFirst Then
< other code goes here >
ElseIf boolSecond Then
< some other code goes here >
ElseIf boolThird Then
< some other code goes here >
ElseIf lngTest = 1 Then
< some other code goes here >
ElseIf strName = ―Bill‖ Then< some other code goes here >
End If

4.1.2 Select Case branches

Select...End Select The syntax is as follows:

Select Case < expression >
Case <possibility 1>
< code goes here >
Case <possibility 2>
< other code goes here >
Case <possibility 3>
< other code goes here >
Case <possibility n>
< other code goes here >
Case Else
< other code goes here >
End Select

注意:This is repeated evaluation of the same expression, and If...ElseIf...End Ifcan be different expressions. Also note that after the completion of all tests, can also have an optional Case Else, if other options are not returned Truewill execute this block of code when

4.2 loop structure

4.2.1 For…Next

For…NextCycle in two situations:

  • The number of code executed repeatedly known.
  • Complex data structures such as arrays, files or database tables, etc. Each element performs a code (but For Each…Nextloop is designed for another complex data structures, collection (Collection) Design). Let's look at how to use the For ... Next loop will execute a piece of code several times
Option Explicit
Dim lngIndex
For lngIndex = 1 To 5
MsgBox "Loop Index: " & lngIndex
Next

First, pay attention, to use For…Nextthe cycle requires a loop variable - that is, the loop index ( loop index). lngIndexThis feature is variable. Statement For lngIndex = 1 To 5means that the loop will execute 5 times. You can see from these dialog boxes, the value lngIndex progressively increases from 1 to 5. Upon completion of the fifth cycle,
the cycle ends, the code continues to run. Note that does not necessarily begin from 1 5 cycles (FOR_LOOP_NOT_ ONE.VBS)
can also use Stepkeywords to skip some numbers, that is, every step of the growth in value

4.2.1.1 out of the loop

Exit For Statement at any time terminate the loop

4.2.2 For Each…Next

For Each…NextLoop is a special cycle, the collection designed to traverse. Collection (Collection), as the name implies, is a collection of data, much like an array. Generally contain collection objects (although the data sets may also be stored in various types) the same type.
For example, VBScript built-in runtime objects FileSystemObjectis Folderobject representing the file system directory. Files Folder object has a collection of performance of a property. There may be several Folder.Files File object in the collection. It can be used For Each…Nextto loop through Folder.Fileseach File object in the collection. For Each…NextYou can not directly control loop cycles

4.2.3 Do Loop

DoIt is the most common circulating loop structure. Because it can be easily used to set the number of cycles in accordance with any desired standard

Do cycle power comes from the While 和 Untilkeyword. While and Until both can be used at the beginning of the cycle, it can also be used at the end of the cycle to control whether to execute a loop again. Look at this simple example using a Do loop

Option Explicit
Dim boolLoopAgainDim lngLoopCount
Dim strResponse
boolLoopAgain = False
lngLoopCount = 0
Do
boolLoopAgain = False
lngLoopCount = lngLoopCount + 1
strResponse = InputBox("What is the magic word?")
If UCase(Trim(strResponse)) = "PLEASE" Then
MsgBox "Correct! Congratulations!"
Else
If lngLoopCount < 5 Then
MsgBox "Sorry, try again."
boolLoopAgain = True
Else
MsgBox "Okay, the word we wanted was "Please"."
End If
End If
Loop While boolLoopAgain

4.2.3.1 to choose between Until and While

whileIs falseto terminate the cycle, is truethe next cycle began, but untiljust the opposite, trueto terminate the cycle, falseis the beginning of the next cycle

4.2.3.2 Do loop jump

Exit DoStatements. With Exit Forsimilar, can be used Exit Doat any time to jump out of Dothe loop. There can be many in the loop Exit Dostatement

4.2.4 While…Wend

While…WendCycles are a version from the old BASIC 和 Visual Basicfashioned grammar. For less common with While…Wendcycling, programmers often prefer to use the Do loop.
This is not to say that you must not use the While…Wendcycle, there are a lot of programmers use it. It runs, simple, and there is no indication that Microsoft will stop supporting it. It is just a little behind the times. In order to maintain the integrity of the content, the following is an While…Wendexample of the syntax

Option Explicit
Dim lngCounter
lngCounter = 0
While lngCounter <= 20
lngCounter = lngCounter + 1
'< other code goes here >
Wend

With Docirculation is not the same, there is no need to use While 或 Until, nor can the conditions placed on the end of the cycle. 判断是否继续循环的条件只能放在循环的开头. Finally, While…Wendthe most important limitation is that it is not equivalent to Exit Foror Exit Dostatements, which means that no forced out of the loop

Published 334 original articles · won praise 186 · views 310 000 +

Guess you like

Origin blog.csdn.net/u012060033/article/details/103349614