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?

7
  • 1
    I think this one contains the answer: stackoverflow.com/questions/943398/enums-returning-int-value Commented Aug 8, 2012 at 13:33
  • 1
    int i = (int)myFlags; then MyFlags myFlags = (MyFlags)i. Works as you seem to ask for. Commented Aug 8, 2012 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. Commented Aug 8, 2012 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 = ...; Commented Aug 8, 2012 at 13:49
  • 1
    'Cause, not seeing the deleted comments/answers, I would answer var selectedAsInt = (int)selected;. Commented Aug 8, 2012 at 14:13

1 Answer 1

4

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.

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

7 Comments

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.
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?"
@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.
Oh, and I think you meant selected & Option_A == Option_A instead of selected & Option_A == selected :) (Similarly for the others)
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.