Making a copy of session objects

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
 
K

Kevin Spencer

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
 
G

Guest

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.
 
K

Kevin Spencer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top