davidlars99
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Chumad's post will work if you change it to:
if ((fruits & Fruits.Banana) == Fruits.Banana) {
//do something
}
Jim
if ((fruits & Fruits.Banana) == Fruits.Banana) {
//do something
}
Jim
lol...three of the same but different answers in three mins! must be a record.
ASKER
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
}
Fruits fruits = Fruits.Apple | Fruits.Orange | Fruits.Banana;
if ((fruits & Fruits.Peach) == Fruits.Peach)
{
//do something
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If this works then you are not suppose to see the message box right?
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
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
ASKER
So, are you getting into the IF block or not?
ASKER
You're right, it works... :)
I did not alter the string.
Jim
Jim
ASKER
That's right, you didn't.
Thanks for your help!!!
Thanks for your help!!!
ASKER
One last thing:
How do I determine if a flagged enum has more than one group?
How do I determine if a flagged enum has more than one group?
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);
}
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);
}
ASKER
what about this?
if ((int)selection) > 1)
{
// multiple selection
}
if ((int)selection) > 1)
{
// multiple selection
}
if (Convert.ToBoolean(fruits & Fruits.Banana))
{
// do something
}