C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Enum in a Switch Statement


Posted Date: 23 May 2008    Resource Type: Code Snippets    Category: C# Syntax

Posted By: shakti singh tanwar       Member Level: Diamond
Rating:     Points: 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


Feedbacks      
Popular Tags   What are tags ?   Search 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!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

audio conferencing services

Contact Us    Privacy Policy    Terms Of Use