Public Class singleton
'Name that will be used as key for Session object
Private Const SESSION_SINGLETON As String = "SINGLETON"
'Variables to store the data (used to be individual
'session key/value pairs)
Dim _lastName As String = ""
Dim _firstName As String = ""
Public Property LastName() As String
Get
Return _lastName
End Get
Set(ByVal Value As String)
_lastName = Value
End Set
End Property
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal Value As String)
_firstName = Value
End Set
End Property
'Private constructor so cannot create an instance
' without using the correct method. This is
' this is critical to properly implementing
' as a singleton object, objects of this
' class cannot be created from outside this
' class
Private Sub New()
End Sub
'Create as a static method so this can be called using
'just the class name (no object instance is required).
'It simplifies other code because it will always return
'the single instance of this class, either newly created
'or from the session
Public Shared Function GetCurrentSingleton() As singleton
Dim oSingleton As singleton
If System.Web.HttpContext.Current.Session(SESSION_SINGLETON) Is Nothing Then
'No current session object exists, use private constructor to
'create an instance, place it into the session
oSingleton = New singleton
System.Web.HttpContext.Current.Session(SESSION_SINGLETON) = oSingleton
Else
'Retrieve the already instance that was already created
oSingleton = CType(System.Web.HttpContext.Current.Session(SESSION_SINGLETON), singleton)
End If
'Return the single instance of this class that was stored in the session
Return oSingleton
End Function
End Class
|