Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .NET: if ((e.State & ListViewItemStates.Selected) != 0) <- What does it mean?

In standard MSN code, there's a line on a ListView - Ownerdraw - DrawItem :

if ((e.State & ListViewItemStates.Selected) != 0)
{
    //Draw the selected background
}

Apparently it does a bitwise comparison for the state ?? Why bitwise ? The following doesn't work:

if (e.State == ListViewItemStates.Selected)
{
    //Doesn't work ??
}

Why doesn't that comparison work ? It's just a standard Enum ?? I'm a bit bedaffled ..

like image 370
Run CMD Avatar asked Dec 02 '22 05:12

Run CMD


2 Answers

It's not a standard Enum - it's decorated with the FlagsAttribute, making it a bitmask. See MSDN FlagsAttribute for details.

The first example checks whether any of the flags is set, as you have rightly interpreted. Flags are generally combined using the | operator (though + and ^ are also safe for a properly specified attribute with no overlaps).

like image 68
David M Avatar answered Dec 03 '22 18:12

David M


It's possible to use Enumeration Types as Bit Flags.

like image 43
dtb Avatar answered Dec 03 '22 18:12

dtb