Solved

Flagged enum equation logic

Posted on 2007-11-13
16
301 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now