Saving Classes in Session Variables

  • Thread starter Thread starter Munsifali Rashid
  • Start date Start date
M

Munsifali Rashid

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
 
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:

Got it - it works perfectly now!

Thanks very much for your help!

Fred

Munsifali Rashid said:
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 said:
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 said:
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

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
(e-mail address removed)
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 said:
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
 
Ah yes, my mistake. Thanks Adam :-)

Mun



Adam Clauss said:
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).
 
Back
Top