I am trying to use a GoTo statement but it says "Error 1 Labels are not valid outside methods."

My code is like this:

Public Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
Start:
         Dim ......
End Sub

Public Sub Retry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Retry.Click 
      GoTo Start
End Sub

I know it works when I do this:

Public Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click 
Start:
        Dim.....
GoTo Start
End Sub

but that just stalls the program cause it's going in loops. How do I make this work?

GoTo only works within a method, you can't jump between methods. Maybe if you describe why you want to do this, we can offer a better solution.

It's not good practice, but if must, you can direct the execution flow by calling "Start_Click" from with "Retry_Click" i.e. replace

Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
Start:
         Dim ......
End Sub

Public Sub Retry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Retry.Click 
      GoTo Start
End Sub

with

Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
Start:
         Dim ......
End Sub

Public Sub Retry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Retry.Click 
      Call Start_Click(Sender,e)
End Sub

A better solution would be to create a procedure to handle the logic and make it accessible to both methods
i.e.

Sub MyProc(ByVal sender As System.Object, ByVal e As System.EventArgs)
   ' Do your stuff
end sub

Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
    Call MyProc(sender,e)
End Sub

Public Sub Retry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Retry.Click 
       Call MyProc(sender,e)
End Sub

Hope that helps

This article has been dead for over six months. Start a new discussion instead.