Question on Enum flags

  • Thread starter Thread starter BillG
  • Start date Start date
B

BillG

I have the following defined.

enum DisplayEventType{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventType dEventType;

if(DisputesCheckBox.Checked)
dEventType = dEventType & DisplayEventType.DISPUTE;
if(ActionCheckBox.Checked)
dEventType = dEventType & DisplayEventType.ACTION;
if(QuestionCheckBox.Checked)
dEventType = dEventType & DisplayEventType.QUESTION;



later on in my code I want to know if its a dispute, a question or an action
or any combination of the 3

how do I do that?




BillG
 
BillG said:
I have the following defined.

enum DisplayEventType{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION =
0x0004);
DisplayEventType dEventType;

if(DisputesCheckBox.Checked)
dEventType = dEventType & DisplayEventType.DISPUTE;
if(ActionCheckBox.Checked)
dEventType = dEventType & DisplayEventType.ACTION;
if(QuestionCheckBox.Checked)
dEventType = dEventType & DisplayEventType.QUESTION;



later on in my code I want to know if its a dispute, a question or an
action or any combination of the 3

how do I do that?




BillG

//
if ((dEventType & DisplayEventType.ACTION) == DisplayEventType.ACTION)
DoSomething();
//

From wikipedia (http://en.wikipedia.org/wiki/Bitwise_operation):

"A bitwise AND takes two binary representations of equal length and performs
the logical AND operation on each pair of corresponding bits. In each pair,
the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise,
the result is 0. For example:

0101
AND 0011
= 0001
"

Basically, if the dEventType has all three values set, and you AND it with
one of those values (ACTION), it will return the value with which you AND it
(ACTION). All bits will return 0 except for the ACTION bit, because only
the ACTION bit is represented in both dEventType and
DisplayEventType.ACTION.
 
Use OR not And might help you,

enum DisplayEventType { WALASHE = 0x0000, DISPUTE = 0x0001, ACTION = 0x0002,
QUESTION = 0x0004 };


DisplayEventType dEventType= DisplayEventType.WALASHE; // Initialize it.

if(DisputesCheckBox.Checked)
dEventType = dEventType | DisplayEventType.DISPUTE;
if(ActionCheckBox.Checked)
dEventType = dEventType | DisplayEventType.ACTION;
if(QuestionCheckBox.Checked)
dEventType = dEventType | DisplayEventType.QUESTION;


LVP
 
but what if I want to know if both disputes and questions are in there or
even all three because I want to filter records depending on whether one or
more or all the boxes are checked.

BillG
 
Sorry that was a type, in my source code I do have | instead of &.

Bill
 
I'm sure there are a number of ways to disect a bitwise enumeration, but I
don't claim to be an expert on bits. Depending on how your filtering is
done (is it direct SQL to a db, LINQ, dataview filter?), there might be a
clever way of setting it up.
 
BillG said:
but what if I want to know if both disputes and questions are in there or
even all three because I want to filter records depending on whether one or
more or all the boxes are checked.

BillG

To check for two types:
if ((dEventType & (DisplayEventType.DISPUTE |
DisplayEventType.QUESTION)) == DisplayEventType.DISPUTE |
DisplayEventType.QUESTION) { }

In a more readable way:

DisplayEventType filter = DisplayEventType.DISPUTE |
DisplayEventType.Question;
if (dEventType & filter == filter) { }
 

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

Similar Threads

Pinvoke Signature needed,... 3
enum questions 2
Problem plaing Wav on CE5 7
Sound 8
[Flags] on enum declarations 4
Enum or a class 3
Enum and Properties.Settings 5
Question about PInvokeStackImbalance? Thanks 2

Back
Top