Weird PropertyBag problem, values won't change
- From: "Jonathan" <jvstech@xxxxxxxxx>
- Date: 24 Jan 2006 14:21:28 -0800
I'm in the process of writing a couple server programs that use
sessions to control what goes on with users. Instead of using classes
or structures to store this information, I'm using PropertyBags for two
reasons: 1) I can store the data in a file if need be, and 2) I can
dynamically add values as needed. Since all the values being stored are
scalars (no objects of any kind, all booleans, strings, integers,
etc.), I figured this would make life much easier. One of the values
used is a Timeout value to kick users off that have been idle for too
long. I have two classes:
CSession - Data storage class for a session. This is basically a
wrapper for the PropertyBag object.
CSessions - Collection of CSession objects for all logged in users.
Here's my code for CSession:
---------------------------------------------------------------
Option Explicit
Private m_Data As New PropertyBag
Private m_ID As String
Public Property Let ID(ByVal vData As String)
m_ID = vData
End Property
Public Property Get ID() As String
ID = m_ID
End Property
Public Property Get Item(ByVal Key As String) As String
On Error Resume Next
Item = m_Data.ReadProperty(Key)
On Error GoTo 0
End Property
Public Property Let Item(ByVal Key As String, ByVal Data As String)
m_Data.WriteProperty Key, Data
End Property
---------------------------------------------------------------
Fairly simple, right?
And the code for CSessions:
---------------------------------------------------------------
Option Explicit
'local variable to hold collection
Private mCol As Collection
Public Function Add(ID As String) As CSession
'create a new object
Dim objNewMember As CSession
Set objNewMember = New CSession
'set the properties passed into the method
objNewMember.ID = ID
mCol.Add objNewMember, ID
'return the object created
Set Add = objNewMember
Set objNewMember = Nothing
End Function
'// Item -- returns a given CSession object. This code was originally
'// generated from the VB Class Builder wizard. However, in an effort
to be
'// more flexible, I have modified it to add a new CSession object if
the
'// requested one doesn't exist. Thus, it is now perfectly legal to
say:
'//
'// CSessions("ThisItem") = "Hello, world!"
'//
'// (if "Hello, World" was a session and not a string) even though
'// "ThisItem" doesn't exist in the collection.
'//
Public Property Get Item(ID As String) As CSession
If Exists(ID) Then
Set Item = mCol(ID)
Else
Set Item = Add(ID)
End If
End Property
Public Property Get Count() As Long
Count = mCol.Count
End Property
Public Sub Remove(ID As String)
mCol.Remove ID
End Sub
'// Exists -- Dirty little hack to see if a specified CSession object
'// exists within the collection.
'//
Public Function Exists(ID As String) As Boolean
On Error Resume Next
Exists = (Not (mCol(ID) Is Nothing))
End Function
Public Property Get NewEnum() As IUnknown
'this property allows you to enumerate
'this collection with the For...Each syntax
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
'creates the collection when this class is created
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
'destroys collection when this class is terminated
Set mCol = Nothing
End Sub
---------------------------------------------------------------
When a user logs in, I call the Add method on a CSessions object with a
automatically generated session ID. They are then immediately given a
Timeout of "now + 30 seconds" or whatever the program calls for. In
other words:
Private Sessions As New CSessions
Sub Login(User)
Dim oSess As CSession
Set oSess = Sessions.Add(NewSessionID)
oSess("Timeout") = DateAdd("s", 30, Now)
'Other session data follows
End Sub
Now, that's all well and good. No problem. I can access the Timeout
value later on via Sessions(SessionID)("Timeout") or any other value
that I store on login.
However, when I go to change any of the values, they don't stay
written. Here's what I mean:
Debug.Print "Current timeout: " & Sessions(SessionID)("Timeout")
UpdateTimeout SessionID
Debug.Print "New timeout: " & Sessions(SessionID)("Timeout")
Sub UpdateTimeout(ByVal SessionID As String)
Debug.Print "Updating timeout on session " & SessionID
Sessions(SessionID)("Timeout") = DateAdd("s", 30, Now)
Debug.Print "Timeout has been changed to " &
Sessions(SessionID)("Timeout")
End Sub
Upon being run, the debug window goes something like this:
Current timeout: 1/17/2006 2:18:43 AM
Updating timeout on session HF2
Timeout has been changed to 1/17/2006 2:19:13 AM
New timeout: 1/17/2006 2:18:43 AM
The timeout is ONLY UPDATED inside the sub procedure. As soon as it
exits, it's reverted back to the way it was before.
Does ANYONE have ANY IDEA what is going on with my code?
.
- Follow-Ups:
- Re: Weird PropertyBag problem, values won't change
- From: Jonathan
- Re: Weird PropertyBag problem, values won't change
- From: Jonathan
- Re: Weird PropertyBag problem, values won't change
- Prev by Date: Re: Pressing F10 Without Activating Form Menu
- Next by Date: Re: Pressing F10 Without Activating Form Menu
- Previous by thread: Re: DataGridView Update after leaving a row
- Next by thread: Re: Weird PropertyBag problem, values won't change
- Index(es):
Relevant Pages
|