We help IT Professionals succeed at work.

Do I Need To Destroy FileSystemObject?

MacRena
MacRena asked
on
Hello Experts,

I am writing a utility that creates several Objects

    Dim objFileSysObj As Object
    Dim objFileObj As Object
    Set objFileSysObj = CreateObject("Scripting.FileSystemObject")
    Set objFileObj = objFileSysObj.Getfile("J:\Inetpub\wwwroot\mywebsite.com\Pub\custfile.DBF")


When everything is all done, do I need to destroy them the way you have to Close and Destroy Database and Recordset objects?

If so, can you please give me the syntax for that?

Thanks,

Mac


Comment
Watch Question

Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
Best practices strongly recommended that every objects that you create, should be destroyed by you.

But in practice, programmers often let VB destroy objects automatically when the object goes out of scope.

In your example you should add :
set objFileObj = Nothing
set objFileSysObj = Nothing
when you do no more require the use of these variables.

If you were using recordset/connection, you need to close them before setting them to nothing (the normal rule is that objects that are giving you an Open method, also gives you a Close or Quit method).
YourRS.Close
Set YourRS = Nothing
YourDB.Close
Set YourDB = Nothing

Author

Commented:

thanks, emoreau!  i thought so, but i wasn't sure