473,385 Members | 1,379 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,385 software developers and data experts.

Enum Flags comparison

Hello,

I've got the following enum:

[Flags]
public enum MyStates
{
None = 0
Saved = 1
New = 2
Changed = 4
Deleted = 8
Moved = 16
}

....

Now I create two variables:

MyStates aValue = MyStates.Changed | MyStates.Moved | MyStates.New
MyStates bValue = MyStates.Changed | MyStates.New

Now i want to check if the values in bValue included in aValue (this
must be true)

But if i create a variable, let's call her cValue:
MyStates cValue = MyStates.Changed | MyStates.Saved

In this case cValue is not included in aValue... But my If statement
returns 1 when I compare aValue and cValue:

if ( (IstWert & SollWert) 0)
{
DoSomeThing();
}

Can somebody tell me, how to solve this problem?

Thanks, Chris

Jul 21 '06 #1
4 7024
Oh, my mistake...
Solved it... It's tooearly ;-)
Ch************@gmx-topmail.de schrieb:
Hello,

I've got the following enum:

[Flags]
public enum MyStates
{
None = 0
Saved = 1
New = 2
Changed = 4
Deleted = 8
Moved = 16
}

...

Now I create two variables:

MyStates aValue = MyStates.Changed | MyStates.Moved | MyStates.New
MyStates bValue = MyStates.Changed | MyStates.New

Now i want to check if the values in bValue included in aValue (this
must be true)

But if i create a variable, let's call her cValue:
MyStates cValue = MyStates.Changed | MyStates.Saved

In this case cValue is not included in aValue... But my If statement
returns 1 when I compare aValue and cValue:

if ( (IstWert & SollWert) 0)
{
DoSomeThing();
}

Can somebody tell me, how to solve this problem?

Thanks, Chris
Jul 21 '06 #2
Oh, my mistake...
Solved it... It's too early ;-)
Ch************@gmx-topmail.de schrieb:
Hello,

I've got the following enum:

[Flags]
public enum MyStates
{
None = 0
Saved = 1
New = 2
Changed = 4
Deleted = 8
Moved = 16
}

...

Now I create two variables:

MyStates aValue = MyStates.Changed | MyStates.Moved | MyStates.New
MyStates bValue = MyStates.Changed | MyStates.New

Now i want to check if the values in bValue included in aValue (this
must be true)

But if i create a variable, let's call her cValue:
MyStates cValue = MyStates.Changed | MyStates.Saved

In this case cValue is not included in aValue... But my If statement
returns 1 when I compare aValue and cValue:

if ( (IstWert & SollWert) 0)
{
DoSomeThing();
}

Can somebody tell me, how to solve this problem?

Thanks, Chris
Jul 21 '06 #3
Hi,

You should not include a value for 0 , if you want to check for it , how do
you know if it was None ?
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<Ch************@gmx-topmail.dewrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
Hello,

I've got the following enum:

[Flags]
public enum MyStates
{
None = 0
Saved = 1
New = 2
Changed = 4
Deleted = 8
Moved = 16
}

...

Now I create two variables:

MyStates aValue = MyStates.Changed | MyStates.Moved | MyStates.New
MyStates bValue = MyStates.Changed | MyStates.New

Now i want to check if the values in bValue included in aValue (this
must be true)

But if i create a variable, let's call her cValue:
MyStates cValue = MyStates.Changed | MyStates.Saved

In this case cValue is not included in aValue... But my If statement
returns 1 when I compare aValue and cValue:

if ( (IstWert & SollWert) 0)
{
DoSomeThing();
}

Can somebody tell me, how to solve this problem?

Thanks, Chris

Jul 21 '06 #4
"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us>
wrote in news:OJ**************@TK2MSFTNGP04.phx.gbl:
You should not include a value for 0 , if you want to check for it ,
how do you know if it was None ?

I disagree... including a value for 0 is quite useful.

You can check for it very simply:

MyStates state = MyStates.None;
if (state == MyStates.None) { ... }

Now obviously, this isn't the general way you would check for the non-zero
flags, which I usually do like this (is there a better way??):

if ((state & MyStates.Saved) == MyStates.Saved) { ... }

As for the OP question, Chris, you shouldn't check if the result is 0,
you should check one of:

if (aValue & cValue == aValue) - then aValue is a subset of cValue
if (aValue & cValue == cValue) - then cValue is a subset of aValue

if either one doesn't evaluate, but the result 0, then it means that they
have some common elements, but some different elements as well (this is the
case you are seeing currently.)

-mdb
Jul 21 '06 #5

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...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
3
by: AliR \(VC++ MVP\) | last post by:
Hi Everyone, I have writing a class that represents a object on a screen. The object has normal things like X,Y, Width, Height, ZOrder, and it also has drawing attributes and flags. It has...
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
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.