Solved

switch statement using an enum

Posted on 2007-11-30
1
2,715 Views
Last Modified: 2012-05-05
Can you not use a straight enum in a switch statement?

It seems i have to cast it:

    public enum blah
    {
        p,b,s
    }

    public void dosomething(blah h)
    {
        switch (h)  // CANNOT DO THIS?
        {
            case 1:
                break;
            case 2:
                break;
            default:
                break;
        }


        switch ((int)h)  // THIS IS OKAY
        {
            case 1:
                break;
            case 2:
                break;
            default:
                break;
        }

    }

0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20385545
you can, but you have to use the enum names instead of numerical values in the case part...

 public void dosomething(blah h)

    {
        switch (h)  
        {
            case p:
                break;
            case b:
                break;
            default:
                break;
        }

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question