Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i exit sub but don't continue to next one

Status
Not open for further replies.

johnc83

Technical User
Jan 29, 2008
154
GB
Hi all,

this is surely really simple but I cannot find any info on it.

I would like to know how to exit out of a procedure and request that it ends there (i.e it doesn't go back to the original caller and doesn't go onto to the next sub in the list)

I tried 'Exit Sub' but it still returns to the original caller and I tried simply 'end' but it closed my entire application.

Hope someone can help

Thanks
John

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
chrissie:
Not a good idea - I agree
Not possible anyway...

What about:

Code:
	Private Sub NeverEscape()

		MessageBox.Show("I'm here!!")

NoWayOut:
		GoTo NoWayOut

	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		NeverEscape()
		MessageBox.Show("I escaped")

	End Sub

[wink]

[vampire][bat]
 
You want to die or what. That's horrible.

BTW won't that get you in an endless loop?

Christiaan Baes
Belgium

My Blog
 
How about this...

Code:
Private Function MySubFunction() as Boolean

   Dim blnEscape as Boolean

   blnEscape = True
   
   'Do this, do that, set blnEscape = False if you don't want to quit

   Return blnEscape

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   
   If MySubFunction() Then
      Exit Sub
   Else
      'Do stuff
   End If

End Sub
 
If you ever need exit sub it's time to stop programming and look at your code again. Calling it a code smell is an understatement.

Christiaan Baes
Belgium

My Blog
 
Thanks for the replies people,

I think I'll take another look at it because it seems that if I do get it to work it won't be very efficient.

Chrissie, I had one other procedure which had an 'exit sub' and, taking note of what you have said, I went to look for a workaround but it turns out I didn't even need that bit in the first place.

Would be good if you (or anyone) could explain why it is bad to use. I fully believe you but an explanation would go a long way to bettering my understaning of the language..

Thanks

John

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
from article reference by Craig0201:

Don't forget : in a language that supports exceptions, (almost) no function has a single exit point. Any function call might be an exit point, if an exception is thrown.

[wink]

[vampire][bat]
 
Thank you

John

.NET 2.0, Visual Studio 2005, SQL Server 2005 Express
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top