CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Guest

    THE END FUNCTION

    Can someone explain how and when to use the END function please?


  2. #2
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: THE END FUNCTION

    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.


  3. #3
    Join Date
    Feb 2000
    Posts
    11

    Re: THE END FUNCTION

    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



  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: THE END FUNCTION

    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

  5. #5
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: THE END FUNCTION

    Well put. End is EVIL


  6. #6
    Join Date
    Jan 2000
    Posts
    264

    Re: THE END FUNCTION

    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







  7. #7
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: THE END FUNCTION

    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





  8. #8
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: THE END FUNCTION

    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

  9. #9
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: THE END FUNCTION

    Isn't 'Undocumented Feature' the code name for 'bug' at M$?


  10. #10
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: THE END FUNCTION



    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured