14

Possible Duplicate:
Enums returning int value
How to get the numeric value from the Enum?

THIS IS NOT A DUPLICATE OF EITHER OF THESE, THANKS FOR READING THE QUESTION MODS

Suppose I have a number of my flags enum items selected:

[Flags]
public enum Options
{
    None = 0,
    Option_A = 1,
    Option_B = 2,
    Option_C = 4,
    Option_D = 8,
}

Options selected = Options.Option_A | Options.Option_B;

The value of selected should correspond to 3 (i.e. 2 + 1)

How can I get this into an int?

I've seen examples where the selected is cast ToString() and then split() into each option, e.g.

"Option_A | Option_B" --> { "Option_A", "Option_B" },

then reconstituted into the respective Enum, and the values taken from that, but it's a bit messy. Is there a more straight-forward way to get the sum of these values?

Community
  • 1
  • 1
DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • 1
    I think this one contains the answer: http://stackoverflow.com/questions/943398/enums-returning-int-value – James Wood Aug 08 '12 at 13:33
  • `int i = (int)myFlags;` then `MyFlags myFlags = (MyFlags)i`. Works as you seem to ask for. – Adam Houldsworth Aug 08 '12 at 13:43
  • 1
    Within the 8 minutes that it took me to post this and get a coffee I find that it's been answered, deleted, commented on and an re-answered before I got a chance to even think about what was going on. None of the comments or answers or comments addressed the fact that I'm trying to get the integer sum of all selected flags, *not* a single selected enum item. I even specified in the question "Option_A | Option_B" but that was overlooked, it seems, because attempting the suggested answers results in a compiler error: "Cannot convert System.Enum to int". Thanks for closing it. – DaveDev Aug 08 '12 at 13:45
  • @DaveDev That's the error you get if you have `Enum selected = Options.Option_A | Options.Option_B;` and then try to cast to `int`, but your question has `Options selected = ...;` –  Aug 08 '12 at 13:49
  • Do I understand you correctly when I think you want all the values that compose an enum flag mask, as individual ints? Or do you want the int value of `selected` (3)? – J. Steen Aug 08 '12 at 14:04
  • 1
    'Cause, not seeing the deleted comments/answers, I would answer `var selectedAsInt = (int)selected;`. – J. Steen Aug 08 '12 at 14:13
  • Why isn't it a duplicate? Just saying it isn't a duplicate without explaining why isn't helpful. Instead of just saying "this isn't a duplicate", you should explain why the answers to the other questions didn't help you. – Donald Duck Jan 23 '18 at 18:09

1 Answers1

3

There is not much options as just make an if

List<int> composedValues = new .... 
if((selected & Option_A) == Options.Option_A)
    composedValues.Add((int)Options.Option_A);
else if((selected & Option_B) == Options.Option_B)
    composedValues.Add((int)Options.Option_B);
else if(...)

Finally you will get a list of all compositional values of the result in the composedValues list.

If this is not what you're asking for, please clarify.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • As I mentioned as a reply to your comment on my answer: the question asks to get 3, so a direct cast is all that's asked for. I deleted my answer because other questions that this is a duplicate of have perfectly good answers already. –  Aug 08 '12 at 13:38
  • BTW, from the question, "The value of selected should correspond to 3" and "Is there a more straight-forward way to get the sum of these values?" –  Aug 08 '12 at 13:40
  • @hvd: as I didn't see *any* comment from the OP, I still *think* he is asking for the stuff I wrote down in my answer. – Tigran Aug 08 '12 at 13:40
  • 1
    Oh, and I think you meant `selected & Option_A == Option_A` instead of `selected & Option_A == selected` :) (Similarly for the others) –  Aug 08 '12 at 13:41
  • Since the different options can be set at the same time, the `else` must be removed. `if(selected & Option_A == Option_A) ...; if(selected & Option_B == Option_B) ...;` – Olivier Jacot-Descombes Aug 08 '12 at 13:42
  • 1
    Thanks for 1. reading the actual question before voting to close it because you appear to be the only one who did, and 2. taking a crack at the answer. I was hoping for something a little more robust than this but I'll take it as a starting point and see what I can do. If it works for me I'll select it as correct and update with modifications I needed to get it to work. – DaveDev Aug 08 '12 at 13:47
  • @DaveDev: you're welcome. For future, try *clearly* state, what you're asking for. Actually it's not very clear what *exactly* you're asking for here, so many guys who read this, fail in trap :) – Tigran Aug 08 '12 at 13:53