Question : SessionHandler Class behaving very strange

Please take a look at the code I've attached. I've remarked out all my original get and set coding and replaced it with session variables. I spent most of a day trying to get the original code to behave but I ended up having to use the session variable work around.

With the session variable workaround in place I can read or write to my SessionHandler class with no issues but If I remove the session variable coding and enable the original coding that's currently commented out I get some very strange behavior.

The behavior I want is:

SessionHandler.UserName = "user name here"  ' Set the current user name
or
Response.Write(SessionHandler.UserName) ' Output the current user name

This works fine using the session variables but with the original coding If I set any of the variables except UserLevel, all the variables change to match the value.  So if I set the SessionHandler.UserName = "My Name Here" then SessionHandler.BorrowerPin would change to "My Name Here". This makes no sense to me and the only thing I can think of is the Private Shared variables are some how storing information in the same space.

p.s. An except to this rule is the SessionHandler.UserLevel seems to work properly. I've also tried changing the Return String.Empty to double quotes and that made no difference.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
Imports Microsoft.VisualBasic

Public Class SessionHandler
    Private Shared _UserName As String = ""
    Private Shared _UserLevel As String = "0"
    Private Shared _SignInStatus As String = "False"
    Private Shared _BorrowerNumber As String = ""
    Private Shared _BorrowerBarcode As String = ""
    Private Shared _BorrowerType As String = ""
    Private Shared _BorrowerPin As String = ""

    Public Shared Sub Clear()
        SessionHandler.UserName = ""
        SessionHandler.UserLevel = "0"
        SessionHandler.SignInStatus = "False"
        SessionHandler.BorrowerNumber = ""
        SessionHandler.BorrowerBarcode = ""
        SessionHandler.BorrowerType = ""
        SessionHandler.BorrowerPin = ""
    End Sub

    Public Shared Property UserName As String
        Get
            Return HttpContext.Current.Session("Username")
            'If (HttpContext.Current.Session(SessionHandler._UserName) Is Nothing) Then
            'Return String.Empty
            'Else
            'Return HttpContext.Current.Session(SessionHandler._UserName).ToString
            'End If
        End Get

        Set(ByVal value As String)
            HttpContext.Current.Session("Username") = value
            ' HttpContext.Current.Session(SessionHandler._UserName) = value
        End Set
    End Property

    Public Shared Property UserLevel As String
        Get
            Return HttpContext.Current.Session("UserLevel")
            ' If (HttpContext.Current.Session(SessionHandler._UserLevel) Is Nothing) Then
            ' Return "0"
            ' Else
            ' Return HttpContext.Current.Session(SessionHandler._UserLevel).ToString
            ' End If
        End Get

        Set(ByVal value As String)
            HttpContext.Current.Session("UserLevel") = value
            'HttpContext.Current.Session(SessionHandler._UserLevel) = value
        End Set
    End Property

    Public Shared Property SignInStatus As String
        Get
            Return HttpContext.Current.Session("SignInStatus")
            ' If (HttpContext.Current.Session(SessionHandler._SignInStatus) Is Nothing) Then
            ' Return "False"
            ' Else
            ' Return HttpContext.Current.Session(SessionHandler._SignInStatus).ToString
            ' End If
        End Get

        Set(ByVal value As String)
            HttpContext.Current.Session("SignInStatus") = value
            'HttpContext.Current.Session(SessionHandler._SignInStatus) = value
        End Set
    End Property

    Public Shared Property BorrowerNumber As String
        Get
            Return HttpContext.Current.Session("BorrowerNumber")
            ' If (HttpContext.Current.Session(SessionHandler._BorrowerNumber) Is Nothing) Then
            ' Return String.Empty
            ' Else
            ' Return HttpContext.Current.Session(SessionHandler._BorrowerNumber).ToString
            ' End If
        End Get

        Set(ByVal value As String)
            HttpContext.Current.Session("BorrowerNumber") = value
            ' HttpContext.Current.Session(SessionHandler._BorrowerNumber) = value
        End Set
    End Property

    Public Shared Property BorrowerBarcode As String
        Get
            Return HttpContext.Current.Session("BorrowerBarcode")
            ' If (HttpContext.Current.Session(SessionHandler._BorrowerBarcode) Is Nothing) Then
            ' Return String.Empty
            ' Else
            ' Return HttpContext.Current.Session(SessionHandler._BorrowerBarcode).ToString
            ' End If
        End Get

        Set(ByVal value As String)
            HttpContext.Current.Session("BorrowerBarcode") = value
            ' HttpContext.Current.Session(SessionHandler._BorrowerBarcode) = value
        End Set
    End Property

    Public Shared Property BorrowerType As String
        Get
            Return HttpContext.Current.Session("BorrowerType")
            ' If (HttpContext.Current.Session(SessionHandler._BorrowerType) Is Nothing) Then
            ' Return String.Empty
            ' Else
            ' Return HttpContext.Current.Session(SessionHandler._BorrowerType).ToString
            ' End If
        End Get

        Set(ByVal value As String)
            HttpContext.Current.Session("BorrowerType") = value
            ' HttpContext.Current.Session(SessionHandler._BorrowerType) = value
        End Set
    End Property

    Public Shared Property BorrowerPin As String
        Get
            Return HttpContext.Current.Session("BorrowerPin")
            ' If (HttpContext.Current.Session(SessionHandler._BorrowerPin) Is Nothing) Then
            ' Return String.Empty
            ' Else
            ' Return HttpContext.Current.Session(SessionHandler._BorrowerPin).ToString
            ' End If
        End Get

        Set(ByVal value As String)
            HttpContext.Current.Session("BorrowerPin") = value
            ' HttpContext.Current.Session(SessionHandler._BorrowerPin) = value
        End Set
    End Property
    
End Class

Answer : SessionHandler Class behaving very strange

Oh I forgot, if you do this

Response.Write(SessionHandler.BorrowerPin)

You will get "My Name Here" because

SessionHandler._BorrowerPin = ""

Return HttpContext.Current.Session("").ToString
Random Solutions  
 
programming4us programming4us