Solved

switch statement using an enum

Posted on 2007-11-30
1
2,710 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:knowlton
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now