473,382 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Question on Enum flags

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
Jun 27 '08 #1
6 1340
"BillG" <bi*******@charter.netwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>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.

Jun 27 '08 #2
LVP
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
"BillG" <bi*******@charter.netwrote in message
news:#Z*************@TK2MSFTNGP02.phx.gbl...
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

Jun 27 '08 #3
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
"Nathan" <ms********@macgregorfamily.netwrote in message
news:09**********************************@microsof t.com...
"BillG" <bi*******@charter.netwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>>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.

Jun 27 '08 #4
Sorry that was a type, in my source code I do have | instead of &.

Bill

"LVP" <sq**********@hotmail.comwrote in message
news:42**********************************@microsof t.com...
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
"BillG" <bi*******@charter.netwrote in message
news:#Z*************@TK2MSFTNGP02.phx.gbl...
>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


Jun 27 '08 #5
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" <bi*******@charter.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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
"Nathan" <ms********@macgregorfamily.netwrote in message
news:09**********************************@microsof t.com...
>"BillG" <bi*******@charter.netwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>>>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.

Jun 27 '08 #6
BillG wrote:
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
"Nathan" <ms********@macgregorfamily.netwrote in message
news:09**********************************@microsof t.com...
>"BillG" <bi*******@charter.netwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>>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.

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) { }
--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ido Tamir | last post by:
Hi, I have an enum, defined as follows: public enum Flags { ON = 0, OFF = 1 } I want the following xml to be deserialized into that enum, without an
1
by: | last post by:
Would be nice to specify an incremenet or decremenet as an attribute in an enum so I can for example have the following... enum SomeEnum { A = 0, B, // 2 C, // 4 D /6
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
1
by: todorov-fkt | last post by:
Hello everyone, I have a field which is 1 byte long and is used to store different flags (according to specs) - it is the flags bit in the ID3 tag header. So 0xabc00000 represents the byte,...
1
by: Dwight.Dexter | last post by:
I have an enum variable. Is there some way to cast an int or BitArray and get my enum value? I have to communicate with an old C-style sever so I get an array of bits("0","1"). But I'm using an...
4
by: Paul E Collins | last post by:
The help file says that "bit fields can be combined using a bitwise OR operation, whereas enumerated constants cannot", and yet this code works: enum Blah { a, b, c }; // without .... Blah x...
2
by: Ray Cassick \(Home\) | last post by:
I have a function that takes a value is as System.Enum and I want to be able to look at that value and determine if it is a regular enum or an enum that has a <Flag()> attribute set on it. I am...
2
by: Jigar.Patel | last post by:
Hi, I have following enum definition. public enum OperationTypes { All = 1, New = 2, View = 4,
7
by: csharpula csharp | last post by:
Hello, I have methods which refer to some class enumerator .Should I make it pulic or encapsulate it? *** Sent via Developersdex http://www.developersdex.com ***
13
by: At_sea_with_C | last post by:
Consider the following code: #include <stdio.h> #define SOMEVAL 1234 enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW }; void print_val(enum tree_types tree) {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.