Sub Statement

Makes a private procedure or event handler.

Syntax

Usual construct

[ modifiers ] _
Sub procedure_name [ type_list ] [ parameter_list ] [ return_type ]
    [ Where generic_constraints ]
    [ statements ]
End [ Sub ]

Event-handler construct

[ modifiers ] _
Sub procedure_name [ type_list ] [ parameter_list ] Handles event_name
    [ Where generic_constraints ]
    [ statements ]
End [ Sub ]

Parts

modifiers
Optional
  • @Const – The procedure can run at compile-time. This puts many limits on the procedure. See @Const.
  • @Iterator – The procedure implements the iterator pattern. The procedure must have a return type. See @Iterator.
  • @MustUse – Code that calls the procedure must use the return value. See @MustUse.
  • @Shared – The procedure is a member of its container, and not an instance of that container. See @Shared.
  • @Test – The procedure is a unit test. See @Test.
procedure_name
Mandatory name for the procedure.
type_list
Optional one or more names with a comma between each, all between brackets ([ ]). See Type List for more information.
parameter_list
Optional one or more declarators with a comma between each, optionally between parentheses. But if you supply return_type, the parentheses are mandatory. See Parameter List for more information.
return_type
Optional in the usual construct.
Not permitted in the event-handler construct.

Return type

As type
or
In unit
type
A data type
unit
A unit of measure
Handles
Mandatory keyword used with the event-handler construct. See the Handles keyword section for more information.
generic_constraints
Optional. See Where Clause (Generics) for more information.
statements
Optional one or more statements to declare variables and/or run some code.
End
Completes the statement. You can also use End Sub.
You can make one of these mandatory with the directive @Option End. See @Option Directive for more information.

Instructions

End Sub is not the same as Exit Sub.

Sub is short for “subroutine”.

Handles keyword

The keyword Handles does two things for the construct Sub.

  1. It changes the procedure to become an event handler.
  2. It puts the parameters supplied by a declaration of Event into the procedure where they become available.

Examples

TODO

See also