Global Variables

TheWizardofInt

Junior Contributor
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
Normally, I will instantiate a class for global variables with a section like so:

Code:
Private m_FullName As String
    Private m_Identity As System.Security.Principal.WindowsIdentity
    Private m_URL As String

    Shared myInstance As globVars

    Private Sub New()

        m_Identity = System.Security.Principal.WindowsIdentity.GetCurrent()
        m_FullName = m_Identity.Name

    End Sub

    Public Shared Function GetInstance() As globVars
        If myInstance Is Nothing Then
            myInstance = New globVars
        End If
        Return myInstance
    End Function

'/ insert properties here

Then invoke the class:

Dim glob as globVars

glob = globVars.GetInstance

And start using variables.

This works over a network environment, but not so hot in an Internet environment, I believe because the Application doesn't really close if everyone is on it.

I tried setting the class to nothing in the Application.Close event - no luck.

Any ideas as to what I am doing or not doing wrong here?
 
Global variables and asp.net applications are a really difficult mix and you need to be very careful about how you use them.

Application variables will remain until the application exits and it's only then that the application_close event will fire. If you wish to remove the class when a user disconnects then the session_end may be a more appropriate place.

However you will probably still get the potential for problems with your code due to the use of shared data members. It may be easier to design the class to use instance data rather than shared data and then store it in either the application or session based on the requirements of your app.
 
Back
Top