views:

151

answers:

5

Hi, I want to use switch statement in c#. but instead of using a constant in the case expression I want to use an enumeration value. How do I use this. If i try to use it like:

 string strPageMode=...//some value;
 switch (strPageMode)
 {
     case ModesEnum.SystemHealth:
     //some code
     break;
}

giving error. what have to use then ? I don't want to use If statement.

+3  A: 

How is strPageMode declared? It needs to be an instance of ModesEnum, I'm guessing it is a string.

BioBuckyBall
Ask this as a comment next time :)
lasseespeholt
I think it is also the answer.
uriDium
Hmm fair. I guess it depends on what the question really is ;-)
lasseespeholt
ummmmm.... come on please review updated question now please. and please i am expecting focus the concept.
Lalit
@lasseespeholt I normally would, but as uriDium said, I suspected that it was also the answer to his question (as originally stated)
BioBuckyBall
+2  A: 

Assuming that strPageMode represents the name of one possible value of ModesEnum, you could do this:

switch (Enum.Parse(typeof(ModesEnum), strPageMode, false))
{
    ... as before
}
Tim
ummm. not working this...
Lalit
+5  A: 

If it is a string then this will work:

ModesEnum res;

//Implicit generic as opposed to Enum.Parse which returns object
Enum.TryParse(strPageMode, out res); //returns false if parsing failed

switch (res)
{
    case ModesEnum.SystemHealth:
        break;
}

As noted, the generic TryParse is not available in < .Net 4.0. Otherwise, use the Enum.Parse.

lasseespeholt
-1: Did not mention method on available in .NET 4.
leppie
@leppie I see you have created another question about that. I apologize if it have been inconvenient or confusing for you :-/
lasseespeholt
+1: For correcting, btw wasnt my question. All good now :)
leppie
A: 

In one of my projects, I have used switch case in the similar manner.

switch(UserType)
{
   case User.Guest:
            break;
}

Here UserType is a string and User is enum. It worked for me.

Vijay Balkawade
I don't thinks so it worked... my case it is not working. which language u have used?
Lalit
+1  A: 

An enumeration literal (rather than the current value of a variable of that enumeration type) is a constant.

There is a specific sense of the word "generic" in .NET and a general sense in English. I don't understand what this has to do with either.

Based on the quasi-Hungarian name I'm guessing that strPageMode is a string (please tell me you don't really name variables like that in C# code). Considering switch as a sort of syntactic sugar for a set of if-else statements, this means you are doing an equality comparison between a string and an enum. If this were allowed it would be rather pointless, as the string being a string and the enum being an enum, they are inherently never going to be equal whatever their values are.

You need to either parse the string into a ModesEnum value, and use that in the switch statement, or else make the case values strings with ModesEnum.SystemHealth.ToString().

Jon Hanna
Yes sir, I totally agree with this philosophy , that string is string and enum is enum. But to make the generic means If I changes the Enum values ie my ModeEnum items , then there should not impact my class wherever I used this Enum. So the solution is to compare the string which is input to class or its method , check that is that in Enum Or Convert it in Enum. thats it. simply i want to Convert string to Enum type and then I can check. But VS3.5 not support Enum.TryParse() method that helps to perform same convertion. so what I have to do to convert string to Enum type?
Lalit
Any updates sir?
Lalit
`Enum.Parse()`. Meanwhile, make sure that ModeEnum is not publicly visible if you are going to change its values as that will make later versions of the assembly require calling assemblies to be recompiled before they will work (and worse, this won't be obvious, you'll just have bugs). Adding is fine, removing or changing breaks running code.
Jon Hanna
Thanks. but not want to ad ,remove... just need to convert string in enum.
Lalit