Connecting Tech Pros Worldwide Forums | Help | Site Map

Saving Classes in Session Variables

Fred Nelson
Guest
 
Posts: n/a
#1: Nov 17 '05
Hi:

I'm a VB.NET programmer who is attempting to write my first C# web
application. Everything is going VERY well however I have hit one snag:

In the VB.NET world we can easily save classes in session variables. I'm
hoping that I can do the same thing in C# however I'm doing something wrong
since I get the error:

Cannot implicitly convert type 'object' to 'datafunct.sessioninfo'

here is my class in a class library called datafunct:

public class sessioninfo
{
public int memberid;
public string membername;
}

I can load the session variable with:

datafunct.sessioninfo mysession = new datafunct.sessioninfo();
mysession.memberid = 15;
mysession.membername = "Fred Smith";
Session.Contents["sessinfo"] = mysession

I'm not get the error: Cannot implicitly convert type 'object' to
'datafunct.sessioninfo' with the second line of code below:

datafunct.sessiononinfo mysession = new datafunct.sessioninfo();
mysession = Session.Contents["sessinfo"];

Any help for this "newby" question would be GREATLY appreciated!

Thanks very much for your help!

Fred



Munsifali Rashid
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Saving Classes in Session Variables


Good to hear you're making the jump to C#

You need to cast your class back to the correct type, as it is boxed when
you store it into the session. To do this, change the last two lines of
code to read:

datafunct.sessioninfo mysession = (datafunct.sessioninfo)
Session.Contents["sessioninfo"];

ie. You don't need to create a new instance of the class first.

Hope this helps,

Mun

--
Munsifali Rashid
http://www.munit.co.uk/blog/



"Fred Nelson" <fred@smartybird.com> wrote in message
news:OPvr3DPTFHA.3636@TK2MSFTNGP14.phx.gbl...[color=blue]
> Hi:
>
> I'm a VB.NET programmer who is attempting to write my first C# web
> application. Everything is going VERY well however I have hit one snag:
>
> In the VB.NET world we can easily save classes in session variables. I'm
> hoping that I can do the same thing in C# however I'm doing something
> wrong
> since I get the error:
>
> Cannot implicitly convert type 'object' to 'datafunct.sessioninfo'
>
> here is my class in a class library called datafunct:
>
> public class sessioninfo
> {
> public int memberid;
> public string membername;
> }
>
> I can load the session variable with:
>
> datafunct.sessioninfo mysession = new datafunct.sessioninfo();
> mysession.memberid = 15;
> mysession.membername = "Fred Smith";
> Session.Contents["sessinfo"] = mysession
>
> I'm not get the error: Cannot implicitly convert type 'object' to
> 'datafunct.sessioninfo' with the second line of code below:
>
> datafunct.sessiononinfo mysession = new datafunct.sessioninfo();
> mysession = Session.Contents["sessinfo"];
>
> Any help for this "newby" question would be GREATLY appreciated!
>
> Thanks very much for your help!
>
> Fred
>
>[/color]


Fred Nelson
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Saving Classes in Session Variables


Munsifali:

Got it - it works perfectly now!

Thanks very much for your help!

Fred

"Munsifali Rashid" <mun-news@**RemoveToReply**@cordlessmouse.co.uk> wrote in
message news:42728c13$0$2054$db0fefd9@news.zen.co.uk...[color=blue]
> Good to hear you're making the jump to C#
>
> You need to cast your class back to the correct type, as it is boxed when
> you store it into the session. To do this, change the last two lines of
> code to read:
>
> datafunct.sessioninfo mysession = (datafunct.sessioninfo)
> Session.Contents["sessioninfo"];
>
> ie. You don't need to create a new instance of the class first.
>
> Hope this helps,
>
> Mun
>
> --
> Munsifali Rashid
> http://www.munit.co.uk/blog/
>
>
>
> "Fred Nelson" <fred@smartybird.com> wrote in message
> news:OPvr3DPTFHA.3636@TK2MSFTNGP14.phx.gbl...[color=green]
> > Hi:
> >
> > I'm a VB.NET programmer who is attempting to write my first C# web
> > application. Everything is going VERY well however I have hit one snag:
> >
> > In the VB.NET world we can easily save classes in session variables.[/color][/color]
I'm[color=blue][color=green]
> > hoping that I can do the same thing in C# however I'm doing something
> > wrong
> > since I get the error:
> >
> > Cannot implicitly convert type 'object' to 'datafunct.sessioninfo'
> >
> > here is my class in a class library called datafunct:
> >
> > public class sessioninfo
> > {
> > public int memberid;
> > public string membername;
> > }
> >
> > I can load the session variable with:
> >
> > datafunct.sessioninfo mysession = new datafunct.sessioninfo();
> > mysession.memberid = 15;
> > mysession.membername = "Fred Smith";
> > Session.Contents["sessinfo"] = mysession
> >
> > I'm not get the error: Cannot implicitly convert type 'object' to
> > 'datafunct.sessioninfo' with the second line of code below:
> >
> > datafunct.sessiononinfo mysession = new datafunct.sessioninfo();
> > mysession = Session.Contents["sessinfo"];
> >
> > Any help for this "newby" question would be GREATLY appreciated!
> >
> > Thanks very much for your help!
> >
> > Fred
> >
> >[/color]
>
>[/color]


Adam Clauss
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Saving Classes in Session Variables


"Munsifali Rashid" <mun-news@**RemoveToReply**@cordlessmouse.co.uk> wrote in
message news:42728c13$0$2054$db0fefd9@news.zen.co.uk...[color=blue]
> Good to hear you're making the jump to C#
>
> You need to cast your class back to the correct type, as it is boxed when
> you store it into the session. To do this, change the last two lines of[/color]

Note: Your answer was correct - but this is NOT boxing.
Just to clarify:
Boxing is the conversion of a value type (such as int, double, etc) to an
object class ("boxing" the value inside an object).


--
Adam Clauss
cabadam@tamu.edu
[color=blue]
>
> datafunct.sessioninfo mysession = (datafunct.sessioninfo)
> Session.Contents["sessioninfo"];
>
> ie. You don't need to create a new instance of the class first.
>
> Hope this helps,
>
> Mun
>
> --
> Munsifali Rashid
> http://www.munit.co.uk/blog/
>
>
>
> "Fred Nelson" <fred@smartybird.com> wrote in message
> news:OPvr3DPTFHA.3636@TK2MSFTNGP14.phx.gbl...[color=green]
>> Hi:
>>
>> I'm a VB.NET programmer who is attempting to write my first C# web
>> application. Everything is going VERY well however I have hit one snag:
>>
>> In the VB.NET world we can easily save classes in session variables. I'm
>> hoping that I can do the same thing in C# however I'm doing something
>> wrong
>> since I get the error:
>>
>> Cannot implicitly convert type 'object' to 'datafunct.sessioninfo'
>>
>> here is my class in a class library called datafunct:
>>
>> public class sessioninfo
>> {
>> public int memberid;
>> public string membername;
>> }
>>
>> I can load the session variable with:
>>
>> datafunct.sessioninfo mysession = new datafunct.sessioninfo();
>> mysession.memberid = 15;
>> mysession.membername = "Fred Smith";
>> Session.Contents["sessinfo"] = mysession
>>
>> I'm not get the error: Cannot implicitly convert type 'object' to
>> 'datafunct.sessioninfo' with the second line of code below:
>>
>> datafunct.sessiononinfo mysession = new datafunct.sessioninfo();
>> mysession = Session.Contents["sessinfo"];
>>
>> Any help for this "newby" question would be GREATLY appreciated!
>>
>> Thanks very much for your help!
>>
>> Fred
>>
>>[/color]
>
>[/color]


Munsifali Rashid
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Saving Classes in Session Variables


Ah yes, my mistake. Thanks Adam :-)

Mun



"Adam Clauss" <cabadam@nospam.tamu.edu> wrote in message
news:%23u7IkoRTFHA.612@TK2MSFTNGP12.phx.gbl...[color=blue]
> "Munsifali Rashid" <mun-news@**RemoveToReply**@cordlessmouse.co.uk> wrote
> in message news:42728c13$0$2054$db0fefd9@news.zen.co.uk...[color=green]
>> Good to hear you're making the jump to C#
>>
>> You need to cast your class back to the correct type, as it is boxed when
>> you store it into the session. To do this, change the last two lines of[/color]
>
> Note: Your answer was correct - but this is NOT boxing.
> Just to clarify:
> Boxing is the conversion of a value type (such as int, double, etc) to an
> object class ("boxing" the value inside an object).[/color]


Closed Thread