Sub statements in 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

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.

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.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s