VB.NET GoTo Statement

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

The GoTo statement transfers control unconditionally to a specified line in a procedure.

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

The basic syntax of the GoTo statement looks like as shown below.

GoTo line

Label a Line

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


Labels can be used only on executable statements inside methods.

You cannot use a GoTo statement to branch from outside a For...Next, For Each...Next, SyncLock...End SyncLock, Try...Catch...Finally, With...End With, 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 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
Finally block Never allowed Never allowed

The following example uses the GoTo statement to branch to line labels.

Module GoToStatement
    Public Sub Example1()
        Dim number As Integer = 1
        Dim sampleString As String

        If number = 1 Then
            GoTo Line1
            GoTo Line2
        End If
        sampleString = "Number equals 1"
        GoTo LastLine
        sampleString = "Number equals 2"
    End Sub
End Module

Got any VB.NET Question?