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

Enum flags

I wanted to set up a enum to represent certain properties of an object that
can be combined. So I'm using the FlagAttribute with my enum and adding them
to by objects with | (or). But what is the right way to get them back (i.e.
to check it this object has a certain flag set)? I tried:

(MyObj.Properties & MyProps.Prop1) == MyProps.Prop1

But that doesn't work right. What gives?

Thanks

Matt
Nov 15 '05 #1
3 18889
Matt,
(MyObj.Properties & MyProps.Prop1) == MyProps.Prop1

But that doesn't work right. What gives?


In what way does it not work? How did you define your enum? What's the
value of Prop1?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2
100
Hi Matt
(MyObj.Properties & MyProps.Prop1) == MyProps.Prop1

This is the right way to chack the flags.
The problem I guess is in MyProps enum declaration.
You have to defined like this

[Flags]
enum MyProps
{
Prop1 = 1,
Prop2 = 2,
Prop3 = 4,
Prop4 = 8
//and so on
}

Adding FalgsAttribute attribute doesn't set your enum values to be powers
of 2. If you have overlapping flags that checking may not work.

HTH
B\rgds
100
Nov 15 '05 #3
Yeap, that was it. It just dawned on me a minute ago. Thanks for the
response.

"100" <10*@100.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi Matt
(MyObj.Properties & MyProps.Prop1) == MyProps.Prop1

This is the right way to chack the flags.
The problem I guess is in MyProps enum declaration.
You have to defined like this

[Flags]
enum MyProps
{
Prop1 = 1,
Prop2 = 2,
Prop3 = 4,
Prop4 = 8
//and so on
}

Adding FalgsAttribute attribute doesn't set your enum values to be powers
of 2. If you have overlapping flags that checking may not work.

HTH
B\rgds
100

Nov 15 '05 #4

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,
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
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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.