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

Discussion in 'ASP .Net' started by Don Miller, Feb 5, 2007.

  1. Don Miller

    Don Miller Guest

    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
    Don Miller, Feb 5, 2007
    #1
    1. Advertising

  2. 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_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
    >
    >


    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
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Feb 5, 2007
    #2
    1. Advertising

  3. Don Miller

    Don Miller Guest

    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" <> wrote in message
    news:%...
    > 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_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

    >
    > 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
    Don Miller, Feb 6, 2007
    #3
  4. 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
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Feb 6, 2007
    #4
  5. Don Miller

    Don Miller Guest

    Thanks for your explanation.

    "Göran Andersson" <> wrote in message
    news:%...
    > 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
    Don Miller, Feb 6, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Fox
    Replies:
    0
    Views:
    321
    Jack Fox
    Dec 2, 2003
  2. ESmith
    Replies:
    3
    Views:
    513
    Kevin Spencer
    Oct 19, 2005
  3. Malciah
    Replies:
    8
    Views:
    455
    James Kanze
    Apr 19, 2007
  4. Hanumant Ganpat

    About transferring session variables...

    Hanumant Ganpat, Feb 9, 2010, in forum: ASP .Net
    Replies:
    3
    Views:
    595
    deveti putnik
    Feb 10, 2010
  5. Matthew Lybanon
    Replies:
    1
    Views:
    108
    Hywel
    Jun 2, 2004
Loading...

Share This Page