By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,658 Members | 1,218 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,658 IT Pros & Developers. It's quick & easy.

How to evaluate enum stored in Session in C# ASP.Net

P: n/a
Hi
I have an ASP.Net project in C# I am basing on a ASP.Net VB.Net
project. Trying to use a similar coding technique, I have the
following problem:

I have an enum declared as follows

public enum EditMode
{
Add,
Update
}

Then I store a value in a Session in various places...as an example

Session["EditMode"] = EditMode.Update;
Then, later I am tring to determine the value in an if statement

if (Session["EditMode"] == EditMode.Update)
{
....
}

yet I get an error message
"Operator '==' cannot be applied to operands of type 'object' and
'MyWebApp.MyWebFormClass.EditMode'

I tried

if (Session["EditMode"] == (object)EditMode.Update)
this seems to allow the application to build but it doesn't work

When I do an AddWatch on Session["EditMode"] it says Update as I would
think it would but I'm stuck as to how to get the if statement to
evaluate correctly

Thanks for any help....

Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
you need to cast the Session object back to an enum.
e.g
if (((EditMode)Session["EditMode"]) == EditMode.Update)

Ciaran
<va****@comcast.net> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hi
I have an ASP.Net project in C# I am basing on a ASP.Net VB.Net
project. Trying to use a similar coding technique, I have the
following problem:

I have an enum declared as follows

public enum EditMode
{
Add,
Update
}

Then I store a value in a Session in various places...as an example

Session["EditMode"] = EditMode.Update;
Then, later I am tring to determine the value in an if statement

if (Session["EditMode"] == EditMode.Update)
{
...
}

yet I get an error message
"Operator '==' cannot be applied to operands of type 'object' and
'MyWebApp.MyWebFormClass.EditMode'

I tried

if (Session["EditMode"] == (object)EditMode.Update)
this seems to allow the application to build but it doesn't work

When I do an AddWatch on Session["EditMode"] it says Update as I would
think it would but I'm stuck as to how to get the if statement to
evaluate correctly

Thanks for any help....

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.