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.