views:

100

answers:

2

In this question, I use xor operator between enum with [Flags] attribute as following:

[Flags]
enum QueryFlag
{
  None = 0x1,
  ByCustomer = 0x2,
  ByProduct = 0x4,
  ByDate = 0x8
}
QueryFlag flags = QueryFlag.ByCustomer | QueryFlag.ByProduct;

To add an QueryFlag, of course we should use | operator.

flags |= QueryFlag.ByDate;

To remove one, I have a different way with Dan Tao's answer. I'm using:

flags ^= QueryFlag.ByProduct;

while he is using:

flags &= ~QueryFlag.ByProduct;

Obviously his answer is correct and easy to understand. I thought I made a mistake. But after a deep thought I got:

a,b         a^b         a&(~b)
0,0          0           0
0,1          1           0   //the difference
1,0          1           1
1,1          0           0

And now I knew my mistake. ^ is wrong when you try to remove one item which doesn't exist.

QueryFlag q = QueryFlag.ByCustomer | QueryFlag.ByDate;
//try to remove QueryFlag.ByProduct which doesn't exist in q
q ^ QueryFlag.ByProduct    //equals to add ByProduct to q, wrong!
q & (~QueryFlag.ByProduct) // q isn't changed, remain the original value. correct!

But here I got another question: how can I know if q contains one item? Base on Dan Tao's answer I wrote an extension:

public static bool Contains(this QueryFlag flags, QueryFlag flag)
{
   return (flags & (~flag)) != flags;
}

That is, if flags is not changed after removing flag from flags, we know flag is not in flags! It seems correct when:

(QueryFlag.ByProduct | QueryFlag.ByDate).Contains(QueryFlag.None)   //false
(QueryFlag.ByProduct | QueryFlag.ByDate).Contains(QueryFlag.ByDate)  //true

But in fact:

(QueryFlag.ByProduct | QueryFlag.ByDate).Contains(QueryFlag.ByDate | QueryFlag.ByCustomer) //true, but I suppose it's false

I know the reason why it's false, how can I improve it? It's the first question. The second: I want to make the .Contains generic to more enum with [Flags] attribute.

public static bool Contains<T>(this T flags, T flag) where T : Enum//with [Flags]
{
    return (flags & (~flag)) != flags;
}

Probably it's impossible to constrain T with attribute marked. But even I remove this constraint I get a compile error which says operator ~ can't be applied to type T. Why and how to resolve?

A: 

A better way may be this:

return (flags & mask) == mask;

This will return true if all the flags set in mask are set in flags.

return (flags & mask) != 0;

This will return true is any of the flags set in mask are set in flags.

As for the generic method, you could try constraining the type to struct. This constrains T to being a value type.

public static bool Contains<T>(this T flags, T mask) where T : struct
{
    return (flags & mask) == mask;
}
Andrew Cooper
@Andrew - Now I understand your 2 answers to my first question. Thanks very much. However for the 2nd question, writing the constraint `where T:int` is incorrect, because `int` can't be a constraint of `where`.
Danny Chen
True. I'm still getting my head around type constraints. It appears that you can specify struct as a constraint, which constrains T to being a value type. See edited answer.
Andrew Cooper
Timwi
A: 

Your error lies in this method:

public static bool Contains(this QueryFlag flags, QueryFlag flag)
{
   return (flags & (~flag)) != flags;
}

This returns true whenever flags has any (i.e. at least one) of the flags contained in flag, but I think you want all.

It should read:

public static bool Contains(this QueryFlag flags, QueryFlag flag)
{
   return (flags & flag) == flag;
}

Alternatively, you can just use Enum.HasFlag() which does exactly this. For example:

QueryFlag qf = ...;
if (qf.HasFlag(QueryFlag.ByCustomer))
    // ...
Timwi
Oh! I don't know the `HasFlag` Method! That's exactly what I want. Thanks.
Danny Chen
A reminder for others: `HasFlag` is new in C#4.0
Danny Chen