Solved

Flagged enum equation logic

Posted on 2007-11-13
16
306 Views
Last Modified: 2013-11-07
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
Comment
Question by:davidlars99
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 8

Accepted Solution

by:
Chumad earned 200 total points
ID: 20275309
if ((fruits && Fruits.Banana)  == Fruits.Banana) {
   //do something
}

0
 
LVL 22

Assisted Solution

by:JimBrandley
JimBrandley earned 150 total points
ID: 20275319
It isn't much more difficult. This works:
if ((fruits & Fruits.Banana) > 0)
{
   // We have a bananna or fruit salad.
}

Jim
0
 
LVL 2

Expert Comment

by:JumpsInLava
ID: 20275327
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 “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 22

Expert Comment

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

Jim
0
 
LVL 2

Expert Comment

by:JumpsInLava
ID: 20275335
lol...three of the same but different answers in three mins!  must be a record.
0
 
LVL 13

Author Comment

by:davidlars99
ID: 20275418
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
 
LVL 2

Assisted Solution

by:JumpsInLava
JumpsInLava earned 150 total points
ID: 20275505
:-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
 
LVL 13

Author Comment

by:davidlars99
ID: 20275535
If this works then you are not suppose to see the message box right?
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20275598
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
 
LVL 13

Author Comment

by:davidlars99
ID: 20275647
So, are you getting into the IF block or not?
0
 
LVL 13

Author Comment

by:davidlars99
ID: 20275665
You're right, it works... :)
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20275679
I did not alter the string.

Jim
0
 
LVL 13

Author Comment

by:davidlars99
ID: 20275759
That's right, you didn't.

Thanks for your help!!!
0
 
LVL 13

Author Comment

by:davidlars99
ID: 20275797
One last thing:

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

Expert Comment

by:JimBrandley
ID: 20275971
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
 
LVL 13

Author Comment

by:davidlars99
ID: 20290092
what about this?

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question