Home All Groups Group Topic Archive Search About

Making a copy of session objects

Author
3 Apr 2007 11:54 AM
Abhishek
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

Author
3 Apr 2007 7:02 PM
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

Show quote
"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
Author
4 Apr 2007 5:52 AM
Abhishek
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
>
>
>
Author
4 Apr 2007 11:18 AM
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

Show quote
"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
>>
>>
>>

AddThis Social Bookmark Button