Академический Документы
Профессиональный Документы
Культура Документы
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
Declares the name, parameters, and code that define a Sub procedure.
Syntax
[<attributelist>][Partial][accessmodifier][proceduremodifiers][Shared][Shadows][Async]
Subname[(Oftypeparamlist)][(parameterlist)][Implementsimplementslist|Handleseventlist]
[statements]
[ExitSub]
[statements]
EndSub
Parts
attributelist
Partial
https://msdn.microsoft.com/enus/library/dz1z94ha.aspx 1/8
1/25/2017 SubStatement(VisualBasic)
accessmodifier
Public
Protected
Friend
Private
ProtectedFriend
proceduremodifiers
Overloads
Overrides
Overridable
NotOverridable
MustOverride
MustOverrideOverrides
NotOverridableOverrides
Shared
Shadows
Async
https://msdn.microsoft.com/enus/library/dz1z94ha.aspx 2/8
1/25/2017 SubStatement(VisualBasic)
name
Required. Name of the procedure. See Declared Element Names. To create a constructor procedure for a class, set the name of a Sub procedure to the New
keyword. For more information, see Object Lifetime: How Objects Are Created and Destroyed.
typeparamlist
Optional. List of type parameters for a generic procedure. See Type List.
parameterlist
Optional. List of local variable names representing the parameters of this procedure. See Parameter List.
Implements
Optional. Indicates that this procedure implements one or more Sub procedures, each one defined in an interface implemented by this procedure's containing class
or structure. See Implements Statement.
implementslist
implementedprocedure[,implementedprocedure...]
interface.definedname
Part Description
interface Required. Name of an interface implemented by this procedure's containing class or structure.
Handles
Optional. Indicates that this procedure can handle one or more specific events. See Handles.
https://msdn.microsoft.com/enus/library/dz1z94ha.aspx 3/8
1/25/2017 SubStatement(VisualBasic)
eventlist
eventspecifier[,eventspecifier...]
eventvariable.event
Part Description
eventvariable Required. Object variable declared with the data type of the class or structure that raises the event.
statements
EndSub
Remarks
All executable code must be inside a procedure. Use a Sub procedure when you don't want to return a value to the calling code. Use a Function procedure when you
want to return a value.
https://msdn.microsoft.com/enus/library/dz1z94ha.aspx 4/8
1/25/2017 SubStatement(VisualBasic)
You can define a Sub procedure only at the module level. The declaration context for a sub procedure must, therefore, be a class, a structure, a module, or an interface and
can't be a source file, a namespace, a procedure, or a block. For more information, see Declaration Contexts and Default Access Levels.
Sub procedures default to public access. You can adjust their access levels by using the access modifiers.
If the procedure uses the Implements keyword, the containing class or structure must have an Implements statement that immediately follows its Class or Structure
statement. The Implements statement must include each interface that's specified in implementslist. However, the name by which an interface defines the Sub in
definedname doesn't have to match the name of this procedure in name.
VB
SubmySub(ByValqAsString)
Return
EndSub
The ExitSub and Return statements cause an immediate exit from a Sub procedure. Any number of ExitSub and Return statements can appear anywhere in the
procedure, and you can mix ExitSub and Return statements.
A Sub procedure and a Function procedure can have parameters and perform a series of statements. However, a Function procedure returns a value, and a Sub
procedure doesn't. Therefore, you can't use a Sub procedure in an expression.
You can use the Call keyword when you call a Sub procedure, but that keyword isn't recommended for most uses. For more information, see Call Statement.
Visual Basic sometimes rearranges arithmetic expressions to increase internal efficiency. For that reason, if your argument list includes expressions that call other
procedures, you shouldn't assume that those expressions will be called in a particular order.
https://msdn.microsoft.com/enus/library/dz1z94ha.aspx 5/8
1/25/2017 SubStatement(VisualBasic)
If you mark a procedure with the Async modifier, you can use the Await operator in the procedure. When control reaches an Await expression in the Async procedure,
control returns to the caller, and progress in the procedure is suspended until the awaited task completes. When the task is complete, execution can resume in the
procedure.
Note
An Async procedure returns to the caller when either the first awaited object thats not yet complete is encountered or the end of the Async procedure is reached,
whichever occurs first.
You can also mark a Function Statement with the Async modifier. An Async function can have a return type of Task<TResult> or Task. An example later in this topic shows
an Async function that has a return type of Task<TResult>.
AsyncSub procedures are primarily used for event handlers, where a value can't be returned. An Async``Sub procedure can't be awaited, and the caller of an Async``Sub
procedure can't catch exceptions that the Sub procedure throws.
For more information about Async procedures, see Asynchronous Programming with Async and Await, Control Flow in Async Programs, and Async Return Types.
Example
The following example uses the Sub statement to define the name, parameters, and code that form the body of a Sub procedure.
VB
SubcomputeArea(ByVallengthAsDouble,ByValwidthAsDouble)
'Declarelocalvariable.
DimareaAsDouble
https://msdn.microsoft.com/enus/library/dz1z94ha.aspx 6/8
1/25/2017 SubStatement(VisualBasic)
Iflength=0Orwidth=0Then
'Ifeitherargument=0thenexitSubimmediately.
ExitSub
EndIf
'Calculateareaofrectangle.
area=length*width
'PrintareatoImmediatewindow.
Debug.WriteLine(area)
EndSub
Example
In the following example, DelayAsync is an an Async``Function that has a return type of Task<TResult>. DelayAsync has a Return statement that returns an integer.
Therefore, the function declaration of DelayAsync must have a return type of Task(OfInteger). Because the return type is Task(OfInteger), the evaluation of the
Await expression in DoSomethingAsync produces an integer, as the following statement shows: DimresultAsInteger=AwaitdelayTask.
The startButton_Click procedure is an example of an AsyncSub procedure. Because DoSomethingAsync is an Async function, the task for the call to
DoSomethingAsync must be awaited, as the following statement shows: AwaitDoSomethingAsync(). The startButton_Click``Sub procedure must be defined with
the Async modifier because it has an Await expression.
VB
'ImportsSystem.Diagnostics
'ImportsSystem.Threading.Tasks
'ThisClickeventismarkedwiththeAsyncmodifier.
PrivateAsyncSubstartButton_Click(senderAsObject,eAsRoutedEventArgs)HandlesstartButton.Click
AwaitDoSomethingAsync()
EndSub
PrivateAsyncFunctionDoSomethingAsync()AsTask
DimdelayTaskAsTask(OfInteger)=DelayAsync()
DimresultAsInteger=AwaitdelayTask
'Theprevioustwostatementsmaybecombinedinto
'thefollowingstatement.
'DimresultAsInteger=AwaitDelayAsync()
https://msdn.microsoft.com/enus/library/dz1z94ha.aspx 7/8
1/25/2017 SubStatement(VisualBasic)
Debug.WriteLine("Result:"&result)
EndFunction
PrivateAsyncFunctionDelayAsync()AsTask(OfInteger)
AwaitTask.Delay(100)
Return5
EndFunction
'Output:
'Result:5
See Also
Implements Statement
Function Statement
Parameter List
Dim Statement
Call Statement
Of
Parameter Arrays
How to: Use a Generic Class
Troubleshooting Procedures
Partial Methods
2017 Microsoft
https://msdn.microsoft.com/enus/library/dz1z94ha.aspx 8/8