Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Goto statement in VB.NET

saphirothsaphiroth Posts: 5Member

Is there such a statement in VB that would allow me to jump code like a goTo statement. For example within an if statement if true then goTo a specific block of code or class or form or function?


Comments

  • Xtreme.NETXtreme.NET Posts: 112Member
    Yes, VB still supports goto, although its not advisable to be used everywhere...
    for a start check this out
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmgoto.asp


    [size=1][COLOR=blue]"Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."[i]1Thess. 5:16-18[/i][/color][/size]

  • kainsworthkainsworth Posts: 200Member
    You can avoid using GoTo and still have your code jump out of a loop by calling a procedure (or procedures) from your loop then exiting the Loop, or its parent procedure when done.

    eg.
    [code]
    ' Inside a loop
    :
    If iSomeTest > 1000 then
    ' Do something
    NiftyProcedure
    ' Optionally do something else
    AnotherSub(iSomeTest)
    ' then jump out of the loop or out of this Sub completely
    Exit For
    ' or
    ' Exit Sub
    End If
    [/code]

    Ged
  • iwilld0itiwilld0it Posts: 1,134Member
    :
    : Is there such a statement in VB that would allow me to jump code like a goTo statement. For example within an if statement if true then goTo a specific block of code or class or form or function?
    :
    :
    :
    Goto is only useful for very rare circumstances in VB:

    1. Exit from deeply nested loop

    [code]
    For i As Integer = 1 To 10
    For j As Integer = 1 To 10
    If someCondition = True Then
    Goto AfterLoop
    End If
    Next
    Next
    AfterLoop:
    [/code]

    An "Exit For" statement wont work if you want to completely break from both loops due to a condition in the inner loop.

    2. Simulate the "Continue" statement from other programming languages:

    [code]
    Do While someCondition
    Call DoSomething()
    If someInnerCondition Then
    Goto EndOfLoop
    End If

    Call DoSomethingElse()
    EndOfLoop:
    Loop
    [/code]

    The DoSomethingElse() function is not executed if someInnerCondition is true.

    The latter example will be overkill in the next version of VB.NET, because they finally provide the "Continue" statement as part of the language.

    [code]
    Do While someCondition
    Call DoSomething()
    If someInnerCondition Then
    Continue
    End If

    Call DoSomethingElse()
    Loop
    [/code]

    Even so, you can still change the symantics of your code to avoid using the Goto statement anyways.

    [code]
    Do While someCondition
    Call DoSomething()
    If Not someInnerCondition Then
    Call DoSomethingElse()
    End If
    Loop
    [/code]

    Basically, Goto is usually bad programming and should be avoided. If there is an alternate solution that does not involve Goto, then always pick that solution.


Sign In or Register to comment.