[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

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.
0
davidlars99
Asked:
davidlars99
  • 7
  • 5
  • 3
  • +1
3 Solutions
 
ChumadCommented:
if ((fruits && Fruits.Banana)  == Fruits.Banana) {
   //do something
}

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

Jim
0
 
JumpsInLavaCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Jim
0
 
JumpsInLavaCommented:
lol...three of the same but different answers in three mins!  must be a record.
0
 
davidlars99Author 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
 
JumpsInLavaCommented:
:-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
 
davidlars99Author Commented:
If this works then you are not suppose to see the message box right?
0
 
JimBrandleyCommented:
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
 
davidlars99Author Commented:
So, are you getting into the IF block or not?
0
 
davidlars99Author Commented:
You're right, it works... :)
0
 
JimBrandleyCommented:
I did not alter the string.

Jim
0
 
davidlars99Author Commented:
That's right, you didn't.

Thanks for your help!!!
0
 
davidlars99Author Commented:
One last thing:

How do I determine if a flagged enum has more than one group?
0
 
JimBrandleyCommented:
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
 
davidlars99Author Commented:
what about this?

if ((int)selection) > 1)
{
   // multiple selection
}
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now