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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

End-Statement

Status
Not open for further replies.

amw1

Programmer
Feb 9, 2005
12
US
I've been searching the internet for a solution to a particular VB problem. Came across a few somewhat heated discussions about use of the "end" statement.

Can someone please elaborate?


 

Thanks. That is much clearer. I always use Unload Me and then clean up in the unload event. I didn't even realize you could use End that way and thought maybe I was missing something. I guess I was, but at least I've been doing things correctly.

Thanks
 
Yes, calling End is generally not good practice, as there's a good chance that things aren't cleaned up properly.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Many VB programmers would say: NEVER use END in your code.
They compare END statement to driving a car, and if you want to stop it, you turn the ignition off. It does not stop your car, it kills the engine off. But you are still going at 60 clicks down the highway!

Use Unload.Me, and if you clean all your stuff nicely, your code will terminate by itself. And I think this is the best way.

---- Andy
 
This is from HELP:
END statement terminates execution immediately. Never required by itself but may be placed anywhere in a procedure to end code execution, close files opened with the Open statement and to clear variables.

vladk
 
If you are embedding Office objects, then don't get unloaded with the END statement. If you try to run the app again, it will fail because the object is already created, and can't be created again. You should always unload objects, and then set them to nothing as soon as you are finished with them.

-David
2006 Microsoft Valued Professional (MVP)
 
<The end statement terminates the execution of e.g. a form.

Just to be clear, the End statement terminates the execution of an entire application/process/exe. So, if your application is interacting with another exe/process, that process will continue to run if you don't specifically stop it. End will not call Unload and QueryUnload events, and that's why the problem that David points out occurs.

<Unload.Me

Andy, as written, that will fail to compile. As you probably already know, proper syntax is "Unload Me" without the period. Unload predates the "dot notation" syntax, and as such is termed a "Statement" rather than a "Method" by the doc. Of course, if it were a method, it would be Me.Unload anyway, since dot notation is in the form Object.Method.

I generally use End in my code when I have a fatal error, and as the last statement in my Sub Main procedure. Matter of style, I guess.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top