共用方式為


Sub Procedures 

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.

Each time the procedure is called, its statements are executed, starting with the first executable statement after the Sub statement and ending with the first End Sub, Exit Sub, or Return statement encountered.

You can define a Sub procedure in modules, classes, and structures. It is Public by default, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it.

A Sub procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

Declaration Syntax

The syntax for declaring a Sub procedure is as follows:

[modifiers] Sub subname[(parameterlist)]

' Statements of the Sub procedure.

End Sub

The modifiers can specify access level and information regarding overloading, overriding, sharing, and shadowing. For more information, see Sub Statement (Visual Basic).

For more information, see Sub Statement (Visual Basic).

Parameter Declaration

You declare each procedure parameter similarly to how you declare a variable, specifying the parameter name and data type. You can also specify the passing mechanism, and whether the parameter is optional or a parameter array.

The syntax for each parameter in the parameter list is as follows:

[Optional] [ByVal | ByRef] [ParamArray] parameternameAsdatatype

If the parameter is optional, you must also supply a default value as part of its declaration. The syntax for specifying a default value is as follows:

Optional [ByVal | ByRef] parameternameAsdatatype=defaultvalue

Parameters as Local Variables

When control passes to the procedure, each parameter is treated as a local variable. This means that its lifetime is the same as that of the procedure, and its scope is the entire procedure.

Calling Syntax

You invoke a Sub procedure explicitly with a stand-alone calling statement. You cannot call it by using its name within an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The use of the Call keyword is optional but recommended.

The syntax for a call to a Sub procedure is as follows:

[Call] subname[(argumentlist)]

Illustration of Declaration and Call

The following Sub procedure tells the computer operator which task the application is about to perform, and also displays a time stamp. Instead of duplicating this code at the beginning of every task, the application simply callstellOperatorfrom various places. Each call passes a string in thetaskargument that identifies the task being started.

Sub tellOperator(ByVal task As String)
    Dim stamp As Date
    stamp = TimeOfDay()
    MsgBox("Starting " & task & " at " & CStr(stamp))
End Sub

The following example shows a typical call to tellOperator.

Call tellOperator("file update")

See Also

Tasks

How to: Call a Procedure that Does Not Return a Value
How to: Call an Event Handler in Visual Basic

Reference

Sub Statement (Visual Basic)

Concepts

Procedures in Visual Basic
Function Procedures
Property Procedures
Operator Procedures
Procedure Parameters and Arguments