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

Reusing FSO object variables 2

Status
Not open for further replies.

Tarnish

Technical User
Nov 13, 2006
221
US
Hi all,

This is probably a dumb question, but is there any reason why I can't 're-set' a variable after I'm completely done with using it in it's prior context?

For example, let's say I have these lines (leaving aside variable declarations):

Set obj2FSO = CreateObject("Scripting.FileSystemObject")
Set obj2Folder = objFSO.GetFolder("C:\Something\FolderName")
obj2Folder.copy "D:"

Let's say after those lines run, I really don't need to use that folder in the middle statement again. Given that, can I then do this:

Set obj2Folder = objFSO.GetFolder("C:\NewFolder")
obj2Folder.copy "D:"

or, do I for some reason need to create totally new variables and use those in the second set of code lines?

Thanks for any feedback.
T
 
A paranoid safer way:
...
Set obj2Folder = Nothing
Set obj2Folder = objFSO.GetFolder("C:\NewFolder")
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for the replies. Sort of what I was thinking on both accounts.

T
 
Most objects are like balloons: as soon as there are no hands (object reference variables) holding the "string" they float away into oblivion.

Some caveats: Some objects hold transient state like data that is not persisted until a Close method is called (though many do and most should close on deallocation). Out-of-process objects (like when you automate Word for example) may require you to call Quit, etc. before they'll quiet down and go away.

The reference variable is not the object, just a "hand" for holding and manipulating the object. If you let go (Set to Nothing, exit the variable's scope) you lose contact. When no reference holds the object it goes away (unless it is runing code internally, has a Window loaded, etc.).


For components written in VB6 some of the rules are covered in Visual Basic Component Shutdown Rules and most of them apply to all components.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top