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.

make procedure or function maybe more better. you can call from any events or methods.

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

Thanks for you help, the Call Start works great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.