|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Making a copy of session objectsIs 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. -- Show quoteHTH, 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 "Abhishek" <Abhis***@discussions.microsoft.com> wrote in message news:DE52DC44-EB03-4352-B9E5-28E3CA2A7E1C@microsoft.com... > 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 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. Show quote "Kevin Spencer" wrote: > 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 > > "Abhishek" <Abhis***@discussions.microsoft.com> wrote in message > news:DE52DC44-EB03-4352-B9E5-28E3CA2A7E1C@microsoft.com... > > 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 > > > That is correct. Only value types are assigned by copying. That is why some
classes implement the ICloneable interface. -- Show quoteHTH, 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 "Abhishek" <Abhis***@discussions.microsoft.com> wrote in message news:43C222DA-7FFB-4A7B-8986-8F1CA71CEC63@microsoft.com... > 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. > > "Kevin Spencer" wrote: > >> 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 >> >> "Abhishek" <Abhis***@discussions.microsoft.com> wrote in message >> news:DE52DC44-EB03-4352-B9E5-28E3CA2A7E1C@microsoft.com... >> > 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 >> >> >> |
|||||||||||||||||||||||