Share via


Sub Statement (Visual Basic)

Declares the name, parameters, and code that define a Sub procedure.

[ <attributelist> ] [ Partial ] [ accessmodifier ] [ proceduremodifiers ] [ Shared ] [ Shadows ] 
Sub name [ (Of typeparamlist) ] [ (parameterlist) ] [ Implements implementslist | Handles eventlist ]
    [ statements ]
    [ Exit Sub ]
    [ statements ]
End Sub

Parts

  • attributelist
    Optional. See Attribute List.

  • Partial
    Optional. Indicates definition of a partial method. See Partial Methods.

  • accessmodifier
    Optional. Can be one of the following:

    See Access Levels in Visual Basic.

  • proceduremodifiers
    Optional. Can be one of the following:

  • Shared
    Optional. See Shared.

  • Shadows
    Optional. See Shadows.

  • name
    Required. Name of the procedure. See Declared Element Names.

  • 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
    Required if Implements is supplied. List of Sub procedures being implemented.

    implementedprocedure [ , implementedprocedure ... ]

    Each implementedprocedure has the following syntax and parts:

    interface.definedname

    Part

    Description

    interface

    Required. Name of an interface implemented by this procedure's containing class or structure.

    definedname

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

  • Handles
    Optional. Indicates that this procedure can handle one or more specific events. See Handles.

  • eventlist
    Required if Handles is supplied. List of events this procedure handles.

    eventspecifier [ , eventspecifier ... ]

    Each eventspecifier has the following syntax and parts:

    eventvariable.event

    Part

    Description

    eventvariable

    Required. Object variable declared with the data type of the class or structure that raises the event.

    event

    Required. Name of the event this procedure handles.

  • statements
    Optional. Block of statements to run within this procedure.

  • EndSub
    Terminates the definition of this procedure.

Remarks

All executable code must be inside a procedure. Use a Sub procedure when you do not need to return a value to the calling code. Use a Function procedure when you need to return a value.

You can use Sub only at module level. This means the declaration context for a sub procedure must be a class, structure, module, or interface, and cannot be a source file, namespace, procedure, or block. For more information, see Declaration Contexts and Default Access Levels.

Sub procedures default to public access. You can adjust their access levels with the access modifiers.

Rules

  • Implementation. If this procedure uses the Implements keyword, the containing class or structure must have an Implements statement immediately following its Class or Structure statement. The Implements statement must include each interface specified in implementslist. However, the name by which an interface defines the Sub (in definedname) does not have to be the same as the name of this procedure (in name).

Behavior

  • Returning from a Procedure. When the Sub procedure returns to the calling code, execution continues with the statement following the statement that called it.

    The Exit Sub and Return statements cause an immediate exit from a Sub procedure. Any number of Exit Sub and Return statements can appear anywhere in the procedure, and you can mix Exit Sub and Return statements.

    The following example shows a return from a Sub procedure.

    Sub mySub(ByVal q As String)
        Return
    End Sub 
    
  • Calling a Procedure. A Sub procedure, like a Function procedure, is a separate procedure that can take parameters and perform a series of statements. However, unlike a Function procedure, which returns a value, a Sub procedure cannot be used in an expression.

    You call a Sub procedure by using the procedure name, followed by the argument list in parentheses, in a statement. You can omit the parentheses only if you are not supplying any arguments. However, your code is more readable if you always include the parentheses.

    You can optionally use the Call statement to call a Sub procedure. This can improve the readability of your code.

Troubleshooting

Order of Execution. Visual Basic sometimes rearranges arithmetic expressions to increase internal efficiency. For that reason, if your argument list includes expressions that call other procedures, you cannot rely on them being called in any particular order.

Example

The following example uses the Sub statement to define the name, parameters, and code that form the body of a Sub procedure.

Sub computeArea(ByVal length As Double, ByVal width As Double)
    ' 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.
    Debug.WriteLine(area)
End Sub

See Also

Tasks

How to: Use a Generic Class

Troubleshooting Procedures

How to: Create a Partial Method (Visual Basic)

Concepts

Parameter Arrays

Partial Methods

Reference

Implements Statement

Function Statement (Visual Basic)

Parameter List

Dim Statement (Visual Basic)

Call Statement (Visual Basic)

Of