D
Don Miller
I am using a Session variable to hold a class object between ASP.NET pages
(in VB). In my constructor I check to see if the Session variable exists,
and if it doesn't, I create one and populate it with "Me". But if the
Session variable already exists, I would like the new object be populated
with everything from the object stored in the Session variable. Ideally, I'd
like to retrieve the object from the Session variable and assign it to "Me"
(e.g. Me = , but that is not possible, and instead it seems that I have to
populate the new object property by property (see below). Is there a more
efficient way to "clone" the object held in the Server variable to a new
object?
Thanks.
Imports ....
Public Class SessionVarObject
Private Const SESSION_VARIABLE As String = "SessionVarObject"
Private _name As String
... other private variables
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
saveObjectToSession(Me)
End Set
End Property
Public Sub New()
If IsNothing(HttpContext.Current.Session(SESSION_VARIABLE)) Then
saveObjectToSession(Me)
Else
populateMeFromSession()
End If
End Sub
Private Sub saveObjectToSession(ByVal objToSave)
HttpContext.Current.Session(SESSION_VARIABLE) = objToSave
End Sub
Private Sub populateMeFromSession()
Dim obj As SessionVarObject =
CType(HttpContext.Current.Session(SESSION_VARIABLE), SessionVarObject)
' I would prefer to just use one statement
' Me = obj
' instead of delineating each private variable for each property
Me._name = obj._name
... etc.
End Sub
(in VB). In my constructor I check to see if the Session variable exists,
and if it doesn't, I create one and populate it with "Me". But if the
Session variable already exists, I would like the new object be populated
with everything from the object stored in the Session variable. Ideally, I'd
like to retrieve the object from the Session variable and assign it to "Me"
(e.g. Me = , but that is not possible, and instead it seems that I have to
populate the new object property by property (see below). Is there a more
efficient way to "clone" the object held in the Server variable to a new
object?
Thanks.
Imports ....
Public Class SessionVarObject
Private Const SESSION_VARIABLE As String = "SessionVarObject"
Private _name As String
... other private variables
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
saveObjectToSession(Me)
End Set
End Property
Public Sub New()
If IsNothing(HttpContext.Current.Session(SESSION_VARIABLE)) Then
saveObjectToSession(Me)
Else
populateMeFromSession()
End If
End Sub
Private Sub saveObjectToSession(ByVal objToSave)
HttpContext.Current.Session(SESSION_VARIABLE) = objToSave
End Sub
Private Sub populateMeFromSession()
Dim obj As SessionVarObject =
CType(HttpContext.Current.Session(SESSION_VARIABLE), SessionVarObject)
' I would prefer to just use one statement
' Me = obj
' instead of delineating each private variable for each property
Me._name = obj._name
... etc.
End Sub