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

  • Thread starter Thread starter vanvee
  • Start date Start date
V

vanvee

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....
 
you need to cast the Session object back to an enum.
e.g
if (((EditMode)Session["EditMode"]) == EditMode.Update)

Ciaran
 
Back
Top