Solved

Flagged enum equation logic

Posted on 2007-11-13
16
302 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

20 Experts available now in Live!

Get 1:1 Help Now