Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Flagged enum equation logic

Posted on 2007-11-13
16
Medium Priority
?
322 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 800 total points
ID: 20275309
if ((fruits && Fruits.Banana)  == Fruits.Banana) {
   //do something
}

0
 
LVL 22

Assisted Solution

by:JimBrandley
JimBrandley earned 600 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 600 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

783 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