David, There are two opinions regarding global variables. Some people use them all the time while others do whatever they can to avoid them. In your example, it would make more sense to keep track of a single FSO object rather than recreating and destroying it, especially if it is going to be used in multiple areas throughout your script. Generally, you should keep the scope of your variables local to the functions where they are used, but if you need to access a variable or object from many places, there's nothing wrong with using a global variable here and there.
Regards, Steve -------- Original Message -------- From: David <[email protected]> Sent: Mon Jun 25 22:02:13 EDT 2012 To: [email protected] Subject: yet another Object definition question Throughout my app, I am going to use a given object several times, but in different subs and functions, and with different properties and methods. My techie question here is, what is the most adviceable way, or the official practice here? Should I make a Global definition, and then do my local variants thereof, like: Dim FS: Set FS = CreateObject( "WScript.FileSystemObject") Dim F: Set F = Nothing sub ReadFile( Filename) Set F = FS.OpenTextFile( Filename, 1, False) End Sub 'ReadFile. Sub WriteFile( Filename) Set F = FS.OpenTextFile( Filename, 2, True) End Sub 'WriteFile. Sub EndApp() If Not F Is Nothing Then Set F = Nothing Set FS = Nothing End Sub 'EndAp. Or, should I strictly go for local definitions, like: Dim FS: Set FS = CreateObject( "WScript.FileSystemObject") Sub ReadFile( Filename) Dim F: Set F = FS.OpenTextFile( Filename, 1, False) Set F = Nothing End Sub 'ReadFile. Sub WriteFile( Filename) Dim F: Set F = FS.OpenTextFile( Filename, 2, True) Set F = Nothing End Sub 'WriteFile. Sub EndApp() Set FS = Nothing End Sub 'EndApp. In my first sample, it does make the lesser amount of writing and number of instructions. But is the second sample more "inline with the books"? Does the one sample take less memory than the other? And, is the one more safe to run - less vulnerable - than the other? I somehow have the feeling, that the second sample - with all local definition of the F object, and nullifying it for every time - is a bit more safe run. At the same time, I wonder if the repeated definition of the object does take more time or memory - in any way. OK, I know time might not be the biggest issue, since we are talking only fractions of a second anyway. But what if the above was holding objects that are called hundreds of times, like in a speech handler of some kind? The, even fractions of seconds that you save every time you call an object could maybe have an effect? And, what is the encouraged practice in a matter like this. I want to have the app running as safe and fast as possible, smile. Thanks for any feedback here. -- Stephen Clower App Development & Product Support Gw Micro, Inc. Sent from my phone.
