0

Given an enum type:

enum SOMEENUM 
{
   A = true,
   B = false,
   C = true
}

I want to switch on this like:

public void SWITCHON (SOMEENUM sn) 
{
   switch(s)
   {
      case SOMEENMUM.A : xxxxxxxx.......
   }
}

But this doesn't compile; I guess it's using the bool value of the enum.

I want to do switch on Enum as if there is no value assigned to it.

1
  • Why is the formal parameter "sn" but the switch controller is "s"? Is that a typo, or are you intending them to be different? Commented Apr 16, 2011 at 14:53

3 Answers 3

3

First of all:
Enums in C# do not support bool as value. So It should be integers. If we set 2 property's of enum to the same value we can consider one of them lost. From my understanding what you actually is trying to do is: Somehow flag that 2 property's of enum are equal.
My suggestion:

public enum MyEnum
{
    [Description("true")]
    A = 1,
    [Description("false")]
    B = 2,
    [Description("true")]
    C = 3
}

Extension for Enum which will return bool

 public static class EnumEx
    {
        public static bool GetDescriptionAsBool(this Enum value)
        {
            FieldInfo field = value.GetType().GetField(value.ToString());
            DescriptionAttribute attribute
                    = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                        as DescriptionAttribute;
            if(attribute == null)
            {
                //throw new SomethingWentWrongException();
            }
            return bool.Parse(attribute.Description);
        }
    }

As a result you can switch normally and at any time can check what is your enums boll flag just calling GetDescriptionAsBool method of that instance.

Sign up to request clarification or add additional context in comments.

3 Comments

I suggest you use the DefaultValueAttribute class instead, with true or false as the default value, this way it's typed and add some semantic information IMHO. Or you could also come up with a specific attribute, which is probably the best.
I agree, but I posted this just to point how to solve current issue.Thanks to you for better implementation.
i'M USING SYSTEM.ATTRIBUTE RIGHT NOW. THX.
2

Repeated values are just different names for the same thing. There's no way to tell the difference because enums are stored as the values, not as the names.

As for bool values, you use an if for those instead of a switch.

Comments

0

enums require numerical values but do not have to be set. I suggest just removing leaving it like

enum SOMEENUM 
{ 
   A, 
   B,
   C
}

so

public void SWITCHON (SOMEENUM s) 
{
    switch(s) 
    { 
        case SOMEENMUM.A : ...do stuff...
             break;
        case SOMEENMUM.B : ...do stuff...
             break;
        case SOMEENMUM.c : ...do stuff...
             break;
    }
}

2 Comments

I am a firm believer of setting all values (to a sensible unique value) to help minimize surprise-changes in contracts if the order changes, etc.
This doesn't work for the type of enums mentioned specifically in the title: those with repeated values. You missed the whole point of the question, and proposed bad design at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.