Checking if it contains a flag.

S

Sin Jeong-hun

Hello.

This question may be stupid.

enum MyValues
{
Value1, Value2, Value3, Value4, Value5
}

....in the code...
MyValues test=MyValues.Value1 | MyValues.Value2;

....to check if it contains Value1 flag...
if( (test&MyValues.Value1) == MyValues.Value1) <== HERE
{
Do something.
}

I always did that way but is that the cleanest way? Is there more
shorter expression for that?
 
S

Stanimir Stoyanov

I have always found such expressions to look 'strange' for a simple
operation like this but as far as I know, there is no cleaner way to check
if a flag is present.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info
 
A

Arne Vajhøj

Sin said:
This question may be stupid.

enum MyValues
{
Value1, Value2, Value3, Value4, Value5
}

...in the code...
MyValues test=MyValues.Value1 | MyValues.Value2;

...to check if it contains Value1 flag...
if( (test&MyValues.Value1) == MyValues.Value1) <== HERE
{
Do something.
}

I always did that way but is that the cleanest way? Is there more
shorter expression for that?

It is what MS prescribe:

http://msdn.microsoft.com/en-us/library/cc138362.aspx

I would probably have tested >0.

Arne
 
R

raylopez99

Hello.
Hello.

I always did that way but is that the cleanest way? Is there more
shorter expression for that?

Yes, use bit values and then you can XOR, AND, OR, >>, << the bits.
But beware: professional programmers, of the kind that write chess
programs where speed is essential (you have to search a large part of
a chess tree of moves in five seconds or less) have complained that
this is tedious and error prone. One reason I stay away from even
using enum or bit values, etc at all. Just use integers and comment
your code so the reader knows what integer does what. But that's just
me, a newbie.

Hope this 'helps'.

RL
 
A

Arne Vajhøj

Sin said:
Thank you. I'd never looked that page before but if Microsoft have
explained that way, then maybe that's final. I just wondered if there
could be something like
if( test.Contains(MyValues.Value1) ) <== my imaginary Contains()
method
{
Do something
}

If on .NET 3.5 you could write an extension method.

Se below for inspiration.

Arne

=================================================

using System;

namespace E
{
public static class MyExtensions
{
public static bool Contains(this Enum o, Enum v)
{
return (Convert.ToInt32(o) & Convert.ToInt32(v)) > 0;
}
}
[Flags]
public enum ABC { A, B, C };
public class Program
{
public static void Main(string[] args)
{
ABC o = ABC.A | ABC.C;
Console.WriteLine((o & ABC.A) > 0);
Console.WriteLine((o & ABC.B) > 0);
Console.WriteLine((o & ABC.C) > 0);
Console.WriteLine(o.Contains(ABC.A));
Console.WriteLine(o.Contains(ABC.B));
Console.WriteLine(o.Contains(ABC.C));
Console.ReadKey();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top