Making a copy of session objects

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Is there any way so that I can create a copy of session object...

Dim _mclass As New _MyClass
_mclass.GetSetVar1 = "Value1"
_mclass.GetSetVar2 = "Value2"

Session("A") = _mclass


Dim _gclass As New _MyClass
_gclass = CType(Session("A"), _MyClass)
_gclass.GetSetVar1 = "Value3"

Session("B") = _gclass

Response.Write(CType(Session("A"), _MyClass).GetSetVar1)
Response.Write(CType(Session("A"), _MyClass).GetSetVar2)
Response.Write(CType(Session("B"), _MyClass).GetSetVar1)
Response.Write(CType(Session("B"), _MyClass).GetSetVar2)

If you print the above session values, you will get
value2value3value2value3
 
No, there isn't. Classes are reference types, which means that when you
assign a class to a variable, you are assigning a reference to the class.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Thanks for reply...

Now I can understand here what's the problem here. Here I'm assigning values
to the session vars. So now this session have values of reference contained
inside the class. Now every operation that I perform with this session should
be referred to the same memory location.
 
That is correct. Only value types are assigned by copying. That is why some
classes implement the ICloneable interface.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Back
Top