GoTo Statement


GoTo Statement

Syntax

     GoTo label 


label (required)

A source-code label that appears somewhere in the current procedure

Description

The GoTo statement passes execution to a specified line within a procedure.

Usage at a Glance

  • GoTo can branch only to lines within the procedure where it appears.

  • The GoTo statement cannot jump into one of the following block constructs from a location outside of that block: For...Next, For Each...Next, SyncLock...End SyncLock, try...Catch...Finally, Using...End Using, or With...End With.

  • Within a try...Catch...Finally statement, a GoTo statement can be used to jump out of the entire statement only from the try or Catch blocks, not from the Finally block.

  • Within a try...Catch...Finally statement, a GoTo statement can be used to jump from a Catch block to the try block, but no other inter-block jumps are permitted. Jumps are allowed within the same block.

  • GoTo is frequently used to control program flow within a procedure, a technique that often produces highly unreadable "spaghetti code." Great care and restraint should be taken when using the GoTo statement.

Version Differences

Prior to the 2005 release of Visual Basic, the GoTo statement was sometimes used to skip a portion of a loop and continue immediately with the next iteration.

     For counter = 1 To 10        ' some code here        If (someCondition) Then GoTo NextIteration        ' some more code here     NextIteration:        ' ----- This label is only used to iterate.     Next counter 

In Visual Basic 2005, the new Continue keyword provides a better method for jumping to the next iteration of a loop.

     For counter = 1 To 10        ' some code here        If (someCondition) Then Continue For        ' some more code here     Next counter 

See Also

Continue Statement, On Error Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net