- Local time
- Today, 03:01
- Joined
- Feb 28, 2001
- Messages
- 29,976
I'm no expert, but I was intrigued as well. This is my explanation based on the threads I read.
It heals the issues of the value got wiped somehow due to whatever unforseen reason.
But, don't keep doing the same thing over and over again as a waste of resources. Here's an example (I think, since I implemented it myself).
Code:Public Function GetDomainUsername() As String 'Get the users Windows Domain Login username Static sUser As String If sUser = "" Then GetDomainUsername = CreateObject("WScript.Network").UserName End If GetDomainUsername = sUser End Function
So, I need to know the username for a variety of access reasons. But, I don't want to pull the username each time I need it...but, I also need to make sure I have it if it disappears for reasons unknown. With the construction above, I can use GetDomainUsername() as a variable (because I can trust it's going to have the value I need) without constantly making the call to get the username every time I use it.
The same concept gets applied to system object instantiation so that you always have an instantiation to be used.
At least, that's my understanding...
Static variables or public variables from a general module would have the same effect. Either way, you only need to run the code once from whatever you use as an opening menu. This method has the same problem either way. You have to be sure you trap errors because the moment you have an untrapped error, you run into the Windows Last Chance error handler - the one that gives you the Debug or Reset option. At that point, either your USER gets debug access to your code and structures, or you have to reset the code engine - which resets all variables to initial states like zero or blank. Your "self-healing" code
Oh, there is an error in the self-healing part. It SHOULD read:
Code:
Public Function GetDomainUsername() As String
'Get the users Windows Domain Login username
Static sUser As String
If sUser = "" Then
sUser = CreateObject("WScript.Network").UserName
End If
GetDomainUsername = sUser
End Function