1

How can get the enum values from an int input? Let's say I have this enum below.

[Flags]
public enum Weeks
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
}

public List<Weeks> GetEnumValues(int input)
{
    // Don't know what is the logic here
    // Help is much appreciated
}

Then the output is as follows

Examples:
1.) input = 3; This means Sunday and Monday hence 1 + 2 = 3; This should return List<Weeks> { Sunday, Monday }
2.) input = 20; This means Tuesday and Thursday hence 4 + 16 = 20; This should return List<Weeks> { Tuesday, Thursday }
3.) input = 40; This means Wednesday and Friday hence 8 + 32 = 40; This should return List<Weeks> { Wednesday, Friday }

Thank you in advance.

mercu
  • 121
  • 2
  • 16

3 Answers3

4

You can do this by looping every enum "flag" and checking for each "flag" if the bit is set in your input value. Here is one way to do this:

public List<Weeks> GetEnumValues(int input)
{
    Weeks inputEnum = (Weeks)input;
    var list = new List<Weeks>();

    foreach(var enumFlag in Enum.GetValues(typeof(Weeks)).Cast<Weeks>())
    {
        if (inputEnum.HasFlag(enumFlag))
        {
            list.Add(enumFlag);
        }
    }

    return list;
}
sstan
  • 35,425
  • 6
  • 48
  • 66
  • this should not be a list of enums – Anonymous Duck Sep 20 '16 at 02:11
  • 1
    @Sherlock: I don't understand what you mean. OP is asking for a list of `Weeks` values. – sstan Sep 20 '16 at 02:12
  • 2
    @Sherlock: How do you know what OP actually needs or wants? – sstan Sep 20 '16 at 02:13
  • Thank you very much. This is indeed the answer to my headache. I tried the solution and I get the result I need. – mercu Sep 20 '16 at 02:14
  • Based on the example, days are part of the enum, not the enum itself – Anonymous Duck Sep 20 '16 at 02:14
  • @Sherlock "This should return `List { Sunday, Monday }`" – zerkms Sep 20 '16 at 02:15
  • Intriguing, haha sorry im a little bit curious, let us say I select `Weeks.Monday`, is the return of this still an enum type? – Anonymous Duck Sep 20 '16 at 02:16
  • 2
    @Sherlock: yes, `Weeks.Monday` is of type `Weeks`. That's how enums work. – sstan Sep 20 '16 at 02:17
  • @sstan oww, I thought this should return an integer, sorry about that. The possible solution I think was to receive the input then convert it to binary, then from that binary I will check it's bit position if it's on. The position then corresponds to the correct enum index – Anonymous Duck Sep 20 '16 at 02:19
  • 2
    @mercu: I certainly don't want to tell you what to do, but I notice that you've been on SO for some time now and have asked a number of questions, but have never accepted any of the provided answers. Maybe something to consider. – sstan Sep 20 '16 at 02:27
  • oh, thanks for that. That would really an eye opener. – mercu Sep 20 '16 at 02:35
1

@sstan's answer is correct and I voted as answer. But I just want to share also my short version from his solution.

public List<Weeks> GetEnumValues(int input)
{
    Weeks inputEnum = (Weeks)input;
    return Enum.GetValues(typeof(Weeks)).Cast<Weeks>().Where(x => inputEnum.HasFlag(x)).ToList();
}
mercu
  • 121
  • 2
  • 16
0

Another approach - making use of the fact that ToString on a Flags enum builds a comma separated list of the values. Then we can take that and convert each element back to the enum value via Enum.Parse:

public List<Weeks> GetEnumValues(int input)
{
    return ((Weeks)input)
         .ToString()
         .Split(',')
         .Select(day => Enum.Parse(typeof(Weeks), day))
         .ToList();
}

The choice is yours...

Ian of Oz
  • 116
  • 7