Solved

Flagged enum equation logic

Posted on 2007-11-13
16
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 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