Branches unconditionally to a specified line in a procedure.
Required. Any line label.
GoTo statement can branch only to lines in the procedure in which it appears. The line must have a line label that
GoTo can refer to. For more information, see How to: Label Statements.
GoTo statements can make code difficult to read and maintain. Whenever possible, use a control structure instead. For more information, see Control Flow.
You cannot use a
GoTo statement to branch from outside a
End With, or
End Using construction to a label inside.
Branching and Try Constructions
Finally construction, the following rules apply to branching with the
|Block or region||Branching in from outside||Branching out from inside|
||Only from a
||Only to outside the whole construction|
||Never allowed||Only to outside the whole construction, or to the
||Never allowed||Never allowed|
1 If one
Finally construction is nested within another, a
Catch block can branch into the
Try block at its own nesting level, but not into any other
Try block. A nested
Finally construction must be contained completely in a
Catch block of the construction within which it is nested.
The following illustration shows one
Try construction nested within another. Various branches among the blocks of the two constructions are indicated as valid or invalid.
The following example uses the
GoTo statement to branch to line labels in a procedure.
Sub GoToStatementDemo() Dim number As Integer = 1 Dim sampleString As String ' Evaluate number and branch to appropriate label. If number = 1 Then GoTo Line1 Else GoTo Line2 Line1: sampleString = "Number equals 1" GoTo LastLine Line2: ' The following statement never gets executed because number = 1. sampleString = "Number equals 2" LastLine: ' Write "Number equals 1" in the Debug window. Debug.WriteLine(sampleString) End Sub
Submit and view feedback for