473,238 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

Transferring Object from Session Variable to New Class Object???

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_VARI ABLE)) 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
Feb 5 '07 #1
4 3417
Don Miller wrote:
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_VARI ABLE)) 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

If you already have an instance in the session variable, there is no
reason to create a new instance. Use a factory method:

Public Shared Sub GetInstance() as SessionVarObject
Dim instance as SessionVarObject
instance = HttpContext.Current.Session(SESSION_VARIABLE);
If IsNothing(instance) Then
instance = New SessionVarObject();
saveObjectToSession(Me)
End If
Return instance
End Sub

And make the constructor private, so that the factory method is the only
way to create an instance.

--
Göran Andersson
_____
http://www.guffa.com
Feb 5 '07 #2
Thanks for the pointer. But I am a little confused.

It is my ASPX VB code that will use this class instance (even if derived
from a Session Variable) and use the methods in the class to manipulate the
instance (which again has to be written back to the Session variable). It is
my understanding (I could be VERY wrong) that the methods are not part of
the instance (or is that just when an object is serialized?).

"Göran Andersson" <gu***@guffa.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
Don Miller wrote:
>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_VARI ABLE)) 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_VARIABL E), 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

If you already have an instance in the session variable, there is no
reason to create a new instance. Use a factory method:

Public Shared Sub GetInstance() as SessionVarObject
Dim instance as SessionVarObject
instance = HttpContext.Current.Session(SESSION_VARIABLE);
If IsNothing(instance) Then
instance = New SessionVarObject();
saveObjectToSession(Me)
End If
Return instance
End Sub

And make the constructor private, so that the factory method is the only
way to create an instance.

--
Göran Andersson
_____
http://www.guffa.com

Feb 6 '07 #3
Don Miller wrote:
Thanks for the pointer. But I am a little confused.

It is my ASPX VB code that will use this class instance (even if derived
from a Session Variable) and use the methods in the class to manipulate the
instance (which again has to be written back to the Session variable). It is
my understanding (I could be VERY wrong) that the methods are not part of
the instance (or is that just when an object is serialized?).
The methods aren't part of the instance, they are part of the class.
That means that every instance of the class can use the methods.

When you serialize an object, you turn it into a chunk of data, and that
chunk of data isn't an object so it doesn't have any methods or
propertys or anything. When you deserialize the data it's done by
creating a new object containing the same data as the original object.
That object has access to the same methods as the original object as it
is of the same class.

--
Göran Andersson
_____
http://www.guffa.com
Feb 6 '07 #4
Thanks for your explanation.

"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Don Miller wrote:
>Thanks for the pointer. But I am a little confused.

It is my ASPX VB code that will use this class instance (even if derived
from a Session Variable) and use the methods in the class to manipulate
the instance (which again has to be written back to the Session
variable). It is my understanding (I could be VERY wrong) that the
methods are not part of the instance (or is that just when an object is
serialized?).

The methods aren't part of the instance, they are part of the class. That
means that every instance of the class can use the methods.

When you serialize an object, you turn it into a chunk of data, and that
chunk of data isn't an object so it doesn't have any methods or propertys
or anything. When you deserialize the data it's done by creating a new
object containing the same data as the original object. That object has
access to the same methods as the original object as it is of the same
class.

--
Göran Andersson
_____
http://www.guffa.com

Feb 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Abby | last post by:
I have an application that is written in php/mysql on a Linux platform and need to transfer hosts. This is a session based application and I don't know a lot about php/Linux/mysql, but can get by....
2
by: Boban Dragojlovic | last post by:
I'm building a complex web-based reservations system. Gathering the user's data requires between 8 and 15 pages (depending on which options they are interested in). I use the "Session" object to...
3
by: ESmith | last post by:
I have multi-page form where I pass an object between pages as follows: On each page: // What page to go to next private void btnContinue_Click(object sender, System.EventArgs e) { if...
2
by: ryan.d.rembaum | last post by:
Hello, I am trying to build a standardized login screen that several different web application could use. What I would like is for the login screen to detect where the user has come from. ...
3
by: sista via DotNetMonster.com | last post by:
hi.. just wondering what does this error means? here's my vb code: Imports System.Web.Security Imports System.Data Imports System.Data.OleDb Public Class dpitem Inherits System.Web.UI.Page
8
by: ST | last post by:
Hello everyone, Can anyone help me with this error above when I debug my web app project in vstudio.net?? I can't figure it out! It was working fine for months, and now all of a sudden it's not!!...
11
by: syssyx | last post by:
I have a "CurrentUser" object in session that I want to access from each page of a vb.net website. I can successfully access everything if I include the following at the start of each pageload: ...
3
by: Benton | last post by:
Hi there, Currently I have a rather simple class with a few members: string userName; string password; string centers; bool canModify; I have correspondant GET properties for them in the...
3
by: RSH | last post by:
Hi, I have a situation where I have created an object that contains fields,properties and functions. After creating the object I attempted to assign it to a session variable so i could retrieve...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.