Export (0) Print
Expand All

Sub Statement (Visual Basic)


    [ statements ]
    [ Exit Sub ]
    [ statements ]
End Sub




Optional. See Attribute List.




Optional. Can be one of the following:

See Access Levels in Visual Basic.


Optional. Can be one of the following:


Optional. See Shared.


Optional. See Shadows.


Optional. See Async.











implementedprocedure [ , implementedprocedure ... ]

Each implementedprocedure has the following syntax and parts:







Required. Name by which the procedure is defined in interface.





eventspecifier [ , eventspecifier ... ]

Each eventspecifier has the following syntax and parts:







Required. Name of the event this procedure handles.


Optional. Block of statements to run within this procedure.

End Sub

Terminates the definition of this procedure.


Defining a Sub Procedure




Returning from a Sub Procedure


The following example shows a return from a Sub procedure.

Sub mySub(ByVal q As String)
End Sub 


Calling a Sub Procedure





Async Sub Procedures

By using the Async feature, you can invoke asynchronous functions without using explicit callbacks or manually splitting your code across multiple functions or lambda expressions.

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 Note

An Async procedure returns to the caller when either the first awaited object that’s not yet complete is encountered or the end of the Async procedure is reached, whichever occurs first.

You can also mark a Function Statement (Visual Basic) 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>.

Async Sub 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.

An Async procedure can't declare any ByRef parameters.

For more information about Async procedures, see Asynchronous Programming with Async and Await (C# and Visual Basic), Control Flow in Async Programs (C# and Visual Basic), and Async Return Types (C# and Visual Basic).


    ' Declare local variable. 
    Dim area As Double 
    If length = 0 Or width = 0 Then 
        ' If either argument = 0 then exit Sub immediately. 
        Exit Sub 
    End If 
    ' Calculate area of rectangle.
    area = length * width
    ' Print area to Immediate window.
End Sub

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(Of Integer). Because the return type is Task(Of Integer), the evaluation of the Await expression in DoSomethingAsync produces an integer, as the following statement shows: Dim result As Integer = Await delayTask.

The startButton_Click procedure is an example of an Async Sub procedure. Because DoSomethingAsync is an Async function, the task for the call to DoSomethingAsync must be awaited, as the following statement shows: Await DoSomethingAsync(). The startButton_Click Sub procedure must be defined with the Async modifier because it has an Await expression.

' Imports System.Diagnostics 
' Imports System.Threading.Tasks 

' This Click event is marked with the Async modifier. 
    Await DoSomethingAsync()
End Sub 

Private Async Function DoSomethingAsync() As Task
    Dim delayTask As Task(Of Integer) = DelayAsync()
    Dim result As Integer = Await delayTask

    ' The previous two statements may be combined into 
    ' the following statement. 
    ' Dim result As Integer = Await DelayAsync()

    Debug.WriteLine("Result: " & result)
End Function 

Private Async Function DelayAsync() As Task(Of Integer)
    Await Task.Delay(100)
    Return 5
End Function 

'  Output: 
'   Result: 5
© 2014 Microsoft