VBS Tutorial: VBScript-Sub statement

Source: Internet
Author: User

Sub statement

StatementSubThe process name, parameters, and code that constitutes the subject.

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

Parameters

Public

IndicatesSubThe process can be accessed by all other processes in all scripts.

Default

OnlyClassBlockPublicKeyword used to indicateSubA process is the default method of a class. If more than oneDefaultErrors occur.

Private

IndicatesSubThe process can only be accessed by other processes in the script that declares the process.

Name

SubFollow the naming conventions of standard variables.

Arglist

It indicates thatSubVariable list of process parameters. Multiple variables are separated by commas.

Statements

InSubAny statement group executed by the process subject.

ArglistParameters include the following syntax and parts:

[ByVal | ByRef] varname[( )]

Parameters

ByVal

This parameter is passed by value.

ByRef

This parameter is passed by reference.

Varname

The variable name that represents the parameter, which follows the standard variable naming conventions.

Description

If Public or Private is not explicitly specifiedSubThe process is public by default, that is, they are visible to all other processes in the script.SubThe value of local variables is not retained during the call process.

Cannot be in any other process (for example,FunctionOrProperty Get).SubProcess.

UseExit SubThe statement can be immediately removed fromSubExit during the process. Program continues to execute the callSubThe statement after the statement of the process. You canSubAnyExit SubStatement.

AndFunctionSimilar process:SubA process is an independent process that can obtain parameters, execute a series of statements, and change the value of its parameters. AndFunctionThe difference is that the Function process can return values, whileSubA process cannot be used in an expression.

You can use the process name and follow the corresponding parameter list to callSubProcess. How to callSubFor more information about the process, seeCallStatement.

Be careful SubThe process can be recursive, that is, the process can call itself to complete a given task. However, recursion may cause stack overflow.

InSubVariables used in the process are divided into two types: one is explicitly declared in the process, and the other is not. Variables explicitly declared during the process (UseDimOr equivalent) is always a local variable. Variables that are not explicitly declared in the process are also local unless they are explicitly declared at a higher level outside the process.

Be carefulA process can use a variable that is not explicitly declared in the process, but any script-level defined name with the same name will cause a name conflict. If the undeclared variables referenced in the process are the same as the names of other processes, constants, or variables, the process references the script-level names. To avoid such conflicts, useOption ExplicitStatements can force explicit declaration of variables.

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.