C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Enum in a Switch Statement


Posted Date: 23 May 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: shakti singh tanwarMember Level: Diamond    
Rating: 1 out of 5Points: 10



This one took me crazy and many a time i needed to change my code as i was not able to do this.But here it is now :)

public enum MyEnum
{
option1,
option2,
option3
}

public void MyFoo(string enumName)
{
switch ((MyEnum)Enum.Parse(typeof(MyEnum), enumName.ToString()))
{
case MyEnum.option1:
// Do something
break;
case MyEnum.option2:
// Do Something
break;
case MyEnum.option3:
// Do something
break;
}
}



Responses

Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code is going to help lots of guys.
Ton Thanks to you
Regards,
Kapil



Author: Ankit Bhadana    17 Jun 2008Member Level: Bronze   Points : 0
thanks for the code
Regards
Ankit


Author: Nanak Deep    18 Jun 2008Member Level: Bronze   Points : 2
sir i have executed this program

public enum day_variable
{
var1_for_function=1,
var2_while_function=2,
var3_do_while_function=3,
var3_foreach_function=4,
}

public void function1()
{
day_variable var1 = day_variable.var1_for_function;
Console.WriteLine(var1);
}

nd ur code too

one thig is that:

using enum is better than using plain integers



didnt get the concept, why we are using it,,it just have a pop up at typeing, just only for that

when we access enum
monday=1, ?//why we used 1 here
we have to use enum.monday will return "monday"

whats the benefit ?

sir very confusing, why we are using it?


Author: Nanak Deep    18 Jun 2008Member Level: Bronze   Points : 1
1.why we use enum?

2.in what cases we should use enum?

3.where we use enums?





Author: thebarrett27    20 Jun 2008Member Level: Bronze   Points : 0
nice code
thanks


Author: shekar    16 Mar 2009Member Level: Bronze   Points : 1
say Word.WdBuiltinStyle this is an enum..i want to retrive all th elements from this..can anybody help?


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Excel Sheet and XML Operations
Previous Resource: Negative Currency values According to Specific culture
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use