VBS TUTORIAL: VBScript statement-sub statement _vbs

Source: Internet
Author: User
Tags naming convention

Sub statement

Declares the name of a Sub procedure, its parameters, and the code that constitutes its body.

 [Public [Default]| Private] Sub name [(
  arglist
)]
[statements]
[Exit Sub]
[statements]
End Sub

Parameters

Public

Indicates that a Sub procedure can be accessed by all other procedures in all scripts.

Default

Used only with the Public keyword in a class block to indicate that a Sub procedure is the default method for a class. If more than one Default procedure is specified in the class, an error occurs.

Private

Indicates that a Sub procedure can only be accessed by other procedures in the script that declares the procedure.

Name

The name of the Sub , following the standard variable naming convention.

ArgList

Represents a list of variables to be passed to the arguments of a Sub procedure at call time. Separate multiple variables with commas.

Statements

Any group of statements that are executed within the body of the Sub procedure.

The arglist parameter contains the following syntax and parts:

[ByVal | ByRef] varname[( )]

Parameters

ByVal

Indicates that the parameter is passed by value.

Byref

Indicates that the parameter is passed by reference.

VarName

A variable name that represents a parameter, followed by a standard variable naming convention.

Description

If you do not explicitly specify to use public or Private, the Sub procedure defaults to common, that is, they are visible to all other procedures in the script. The value of a local variable in a Sub procedure is not retained during the call.

You cannot define a Sub procedure in any other procedure , such as aFunction or Property Get.

Use the Exit Sub statement to exit immediately from a sub procedure. The statement after the program continues executing the statement that calls the Sub procedure. Any Exit Sub statement can appear anywhere in the sub procedure.

The similarity to a Function procedure is that aSub procedure is an independent process that takes parameters, executes a series of statements, and can change the values of its parameters. Unlike a function procedure, a Function procedure can return a value, and a Sub procedure cannot be used in an expression.

You can call a Sub procedure by using the procedure name and following the corresponding argument list. See the call statement for detailed instructions on how to invoke a Sub procedure.

Be careful A Sub procedure can be recursive, that is, the procedure can call itself to complete a given task. However, recursion can cause a stack overflow.

Variables used in Sub procedures fall into two categories: one is explicitly declared within a procedure, and the other is not. A variable that is explicitly declared within a procedure (using Dim or equivalent method) is always a local variable. Variables that are not explicitly declared in the procedure are also local, unless explicitly declared at a higher level outside the procedure.

Be careful the process can use variables that are not explicitly declared within a procedure, but a name conflict arises whenever any script-level definition has the same name. If the undeclared variable referenced in the procedure is the same as the name of another procedure, constant, or variable, the procedure is assumed to refer to the script-level name. To avoid this type of conflict, use the Option Explicit statement to force an explicit declaration of a variable.

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.