GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Skip to Ask Learn chat experience
  • Download Microsoft Edge

This view

  • Original page
  • New address

GeezerVison is showing learn.microsoft.com — GoTo Statement - Visual Basic

Read in English Edit

Note

Access to this page requires authorization. You can try signing in or changing directories.

Access to this page requires authorization. You can try changing directories.

GoTo Statement

Branches unconditionally to a specified line in a procedure.

Syntax

GoTo line  

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

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, 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 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

1 If 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.

Graphic diagram of branching in Try constructions

Example

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

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
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.

.NET

Open a documentation issue Provide product feedback

Feedback

Was this page helpful?

No

Additional resources


  • Last updated on 2021-09-15
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.