Click to See Complete Forum and Search --> : THE END FUNCTION


February 29th, 2000, 07:14 PM
Can someone explain how and when to use the END function please?

AndyK
February 29th, 2000, 07:45 PM
NEVER use END function unless it's completely necessary, because END function slows down your computer every time you use it because it doesn't clean up memory blocks, it just terminates execution of the code completely!!! Chris also said that "Stop" button is about the same if not the same as END, so using STOP button probably isn't good idea either.

DFW
March 1st, 2000, 09:59 AM
The end function is useful for terminating an application that runs in unattended mode and has no forms. I use it to run SQL Server Data import Runs to check that the file I am waiting for has arrived. In an application you should always use the unload event of the form and set your variables and objects to nothing

Chris Eastwood
March 1st, 2000, 10:10 AM
I still don't see why you should have to use the END function to close a program that has no forms. A program has a logical start and end point

eg.

Sub Main
.
.
.
End Sub

The program may not end at the 'End Sub' because it has other objects that are open - you should really always try to unload / set to nothing all the objects your program is using to close the program cleanly - you're just asking for more problems later on if you use the 'END' command.

Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Kyle Burns
March 1st, 2000, 10:42 AM
Well put. End is EVIL

gknierim
March 1st, 2000, 02:18 PM
So if I had the following code in each form on the unload event, I would be ok?

'Close connections and clean up memory.
If Not db is nothing then
If (db.State = adStateOpen) then
db.Close ' close connection
set db = nothing ' cleanup memory
End If
End If

Kyle Burns
March 1st, 2000, 02:28 PM
Except that you're only going to Set db to Nothing if it's open. Try

If Not db is nothing then
If (db.State = adStateOpen) then db.Close
set db = nothing 'This will execute whether initially open or closed
End If

Chris Eastwood
March 1st, 2000, 02:32 PM
You should always clean down objects that are owned by the form - so if your 'db' object is private to that form then the unload code would work (Well, you might just want to change it to :


If Not db is nothing then
db.Close
set db = nothing
End if




- as the form is already unloading (hence the unload event) you want to make sure that it cleans up it's objects).


If you've declared the 'db' object as public in the form, then you could be in trouble. If another form / public object in your project has a reference to the 'db' object, then the form will never really be unloaded until all references to that object are cleared.

It's also worthwile doing the following when Unloading forms :


Unload frmSomeForm
set frmSomeForm = nothing




The second line clears down the memory used by VB (it still keeps any static variables in the form in memory when the form has been unloaded). Setting the frmSomeForm to nothing gets around this limitation / 'feature' of VB.

Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Kyle Burns
March 1st, 2000, 02:39 PM
Isn't 'Undocumented Feature' the code name for 'bug' at M$?

Chris Eastwood
March 1st, 2000, 02:45 PM
:)

It is actually documented in the VB documentation (as far back as the original paper manuals - remember them ? - in VB3 I think) - these days you just have to trawl through the MSDN to find out about these 'features'.


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb