开发者

Check 3 enum values in one go?

I have an enum which looks like this:

enum myEnum
{
    field1 = 11,
    field2 = 12,
    field3 = 33
};

In my code I need to say that field1 is 1,field2 is 2 and field3 is 3 according to a variable I have. This variable is either 1 or 2 or 3; it's an int. Can I write that in one line? Something like the following, but shorter...

if(myVar == 1)
    SomeMethod(myEnum.开发者_Python百科field1)
...

Thanks :-)


(myEnum) Enum.Parse(typeof(myEnum), "field" + myVar);

Edit: Come to think of it, you probably want to wrap this in a try/catch (ArgumentException) just in case there isn't a value in the enum for the myVar given, unless you can guarantee this will never happen.

Another Edit: Thinking about it, if there's just three values, you could just do:

myVar == 1 ? myEnum.field1 : myVar == 2 ? myEnum.field2 : myEnum.field3;

However, this treats any value other than 1 or 2 as if it was 3, but if this is guaranteed, it shouldn't be a problem.


If I understand you correctly (and if not, please extend your question as it is not very clear), you want to do something like this:

private static readonly Dictionary<int, MyEnum> _dict = new Dictionary<int, MyEnum> {
   {1, MyEnum.field1},
   {2, MyEnum.field2},
   {3, MyEnum.field3}
};


public MyEnum GetIt(int val)
{
  if (!_dict.ContainsKey(val)) throw new ArgumentException("val");

  return _dict[val];
}


Seems like a switch statement would be better than lots of ifs.

switch (myVar)
{
    case 1:
        // do stuff
        break;
    case 2:
        // do stuff
        break;
    case 3:
        // do stuff
        break;
}


It sounds like what you are looking for is Bitwise Operations. By defining your enum to have only one bit set for each of the values, you can perform several interesting operations, including the one I think you are asking about. To define an enum to use like this you might use something like the following:

   [Flags]                             
    enum myEnum :int
    {
        None     = 0,
        field1   = (1 << 0),      //1     binary: 001
        field2   = (1 << 1),      //2             010   
        field3   = (1 << 2),      //4             100

        anyfield = (1 << 3) -1,   //              111

        field1or2 = (field1 | field2),//          011
        field1or3 = (field1 | field3),//          101
        field2or3 = (field2 | field3),            110  

    }

The syntax for initializing the values of the enum are there to make it easy to look at the list and see that exactly one bit is set and all powers of two are used. To check for multiple values you can use:

        //set to field2or3
        myEnum myvar = myEnum.field2or3;

        //add in field1
        myvar |= myEnum.field1;
        //clear field2
        myvar &= myEnum.field2;

        //true because field1 is set, even though field2 is not
        if ((myvar & myEnum.field1or2) != myEnum.None) 
        {
           //...
        } 

or

if((myvar & (int)myEnum.field1or2) != 0) 

if myvar is an int (C# requires an explicit cast to int, unlike C++). Bitwise operations are a little tricky at first, but with a bit of practice you should be able to figure it out.


Have you tried using a switch statement instead of an if against the value? Try this code, which assumes the enumeration type that you've declared in your question. Cast myVar to an myEnum type in the switch statement and "viola!" instant mapping!:

        var myVar = 11;
        switch ((myEnum)myVar)
        {
            case myEnum.field1:
                //Code will hit here.
                break;
            case myEnum.field2:
                break;
            case myEnum.field3:
                break;
            default:
                break;
        }


Another way (bearing in mind the enum is sorted by its values when you use GetValues):

myEnum blah = (myEnum)Enum.GetValues(typeof(myEnum)).GetValue(myVar - 1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜