Labeling Statements
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Labeling 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.

Note   Labels can be used only on executable statements inside methods.

The following lines of code are labeled with Jump and 120, respectively:

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

Labels may be either valid Visual Basic .NET 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.

To label a line of code

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

See Also

Statements | Statements Overview | Program Structure and Code Conventions

© 2015 Microsoft