# Flagged enum equation logic

HI,

Suppose I have:

[Flags]
enum Fruits
{
None = 0,
Apple = 1,
Orange = 2,
Peach = 4,
Banana = 8
}

and then I initialize a avriable:

Fruits fruits = Fruits.Apple | Fruits.Orange | Fruits.Banana;

how do I then find out if "fruits" variable is equal to "Banana"

It would be really great if I could do something like this:

if (fruits == Fruits.Banana)
{
// do something
}

but unfortunately, it's not that convenient.
LVL 13
###### Who is Participating?

x

Commented:
if ((fruits && Fruits.Banana)  == Fruits.Banana) {
//do something
}

0

Commented:
It isn't much more difficult. This works:
if ((fruits & Fruits.Banana) > 0)
{
// We have a bananna or fruit salad.
}

Jim
0

Commented:
Sorry, not a C# coder, and not sure of the syntax, but with bitwise stuff you want to OR stuff together, and use AND to figure out if it is part of...

if (Convert.ToBoolean(fruits & Fruits.Banana))
{
// do something
}
0

Commented:
Chumad's post will work if you change it to:
if ((fruits & Fruits.Banana)  == Fruits.Banana) {
//do something
}

Jim
0

Commented:
lol...three of the same but different answers in three mins!  must be a record.
0

Author Commented:
this return match too even though Fruits.Peach is not part of the group:

Fruits fruits = Fruits.Apple | Fruits.Orange | Fruits.Banana;

if ((fruits & Fruits.Peach)  == Fruits.Peach)
{
//do something
}

0

Commented:
:-S
you sure?  ...this works for me

public partial class Form1 : Form
{
[Flags]
enum Fruits
{
None = 0,
Apple = 1,
Orange = 2,
Peach = 4,
Banana = 8
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Fruits fruits = Fruits.Apple | Fruits.Orange | Fruits.Banana;
if ((fruits & Fruits.Peach) == Fruits.Peach)
{
MessageBox.Show("hey");
}
}
}
0

Author Commented:
If this works then you are not suppose to see the message box right?
0

Commented:
I do not see this behavior. Running this code:
public enum TestEnum
{
None = 0,
Apple = 1,
Bananna = 2,
Orange = 4,
Pineapple = 8
};

string hello = "We have Banannas";

TestEnum te1 = TestEnum.Apple | TestEnum.Bananna;
if ((te1 & TestEnum.Orange) == TestEnum.Orange)
{
hello = "Now we have a Oranges?";
}

When completed, we still have banannas, and not oranges.

Jim
0

Author Commented:
So, are you getting into the IF block or not?
0

Author Commented:
You're right, it works... :)
0

Commented:
I did not alter the string.

Jim
0

Author Commented:
That's right, you didn't.

0

Author Commented:
One last thing:

How do I determine if a flagged enum has more than one group?
0

Commented:
Something like this will work, cannot think of a better way offhand.

public bool IsSalad( Fruits selection )
{
int count = 0;
if ((selection & Fruits.Apple) == Fruits.Apple)
count++;
if ((selection & Fruits.Banana) == Fruits.Banana)
count++;
etc.
return (count > 1);
}
0

Author Commented:

if ((int)selection) > 1)
{
// multiple selection
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.