Delen via


How to: Label Statements

Statement blocks are made up of lines of code delimited by colons. Lines of code preceded by an identifying string or integer are said to be labeled. Statement labels are used to mark a line of code to identify it for use with statements such as On Error Goto.

Labels may be either valid Visual Basic 2008 identifiers—such as those that identify programming elements—or integer literals. A label must appear at the beginning of a line of source code and must be followed by a colon, regardless of whether it is followed by a statement on the same line.

The compiler identifies labels by checking whether the beginning of the line matches any already-defined identifier. If it does not, the compiler assumes it is a label.

Labels have their own declaration space and do not interfere with other identifiers. A label's scope is the body of the method. Label declaration takes precedence in any ambiguous situation.

Note

Labels can be used only on executable statements inside methods.

To label a line of code

  • Place an identifier, followed by a colon, at the beginning of the line of source code.

    For example, the following lines of code are labeled with Jump and 120, respectively:

    Jump:   FileOpen(1, "testFile", OpenMode.Input)
            ' ...
    120:    FileClose(1)
    

See Also

Concepts

Statements Overview

Declared Element Names

Other Resources

Statements in Visual Basic

Program Structure and Code Conventions