1/25/2017 GoToStatement
GoTo Statement
Visual Studio 2015
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
Branches unconditionally to a specified line in a procedure.
Syntax
GoToline
Part
line
Required. Any line label.
Remarks
The 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.
Note
https://msdn.microsoft.com/enus/library/69whc95c.aspx 1/4
1/25/2017 GoToStatement
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 For...Next, ForEach...Next, SyncLock...EndSyncLock, Try...Catch...Finally, With...EndWith, or Using...End
Using construction to a label inside.
Branching and Try Constructions
Within a Try...Catch...Finally construction, the following rules apply to branching with the GoTo statement.
Block or region Branching in from outside Branching out from inside
Try block Only from a Catch block of the same construction 1 Only to outside the whole construction
Catch block Never allowed Only to outside the whole construction, or to the Try block of the same construction 1
Finally block Never allowed Never allowed
1If one Try...Catch...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 Try...Catch...Finally construction must be contained completely in a Try or 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.
https://msdn.microsoft.com/enus/library/69whc95c.aspx 2/4
1/25/2017 GoToStatement
Valid and invalid branches in Try constructions
Example
The following example uses the GoTo statement to branch to line labels in a procedure.
VB
SubgotoStatementDemo()
DimnumberAsInteger=1
DimsampleStringAsString
'Evaluatenumberandbranchtoappropriatelabel.
Ifnumber=1ThenGoToLine1ElseGoToLine2
Line1:
sampleString="Numberequals1"
https://msdn.microsoft.com/enus/library/69whc95c.aspx 3/4
1/25/2017 GoToStatement
GoToLastLine
Line2:
'Thefollowingstatementnevergetsexecutedbecausenumber=1.
sampleString="Numberequals2"
LastLine:
'Write"Numberequals1"intheDebugwindow.
Debug.WriteLine(sampleString)
EndSub
See Also
Do...Loop Statement
For...Next Statement
For Each...Next Statement
If...Then...Else Statement
Select...Case Statement
Try...Catch...Finally Statement
While...End While Statement
With...End With Statement
2017 Microsoft
https://msdn.microsoft.com/enus/library/69whc95c.aspx 4/4