Solved

switch statement using an enum

Posted on 2007-11-30
1
2,716 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

636 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