Hello, 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)

Thanks :-)

+1  A: 

You can use a very long line or build a translation map.

How would that look like? I wanna avoid the if.
+6  A: 
(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.

Ah, if only there were a mind-reader badge! +1
Jeff Sternal
Awful code in terms of supportability, but perfectly answers the question, so +1.
Yeah, it is very dependent on the values in the enum all being 'field<x>'. If you can't guarantee that, you really need a translation map.
@Jeff: I used to deal with corporate management a lot, it's a learned skill :)
+3  A: 

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];
Hans Kesting
+1  A: 

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

switch (myVar)
    case 1:
        // do stuff
    case 2:
        // do stuff
    case 3:
        // do stuff

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.
            case myEnum.field2:
            case myEnum.field3:
Ryan Hayes
This won't work - MyVar is 1, 2, or 3 and the enum values are 11, 12, and 33. The requirement smells.
Ahh, ok. Thanks for pointing that out. Looks like mapping/parsing is the way to go then.
Ryan Hayes

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);
Alex K.
+1  A: 

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:

    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) 


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.