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

Enum as flags and the switch

Hi,

To exclude users based on a criteria, I have created an enum like so:

[FlagsAttribute]
public enum Exclusions:byte
{
None=0,
HaveEmail=1,
IsAdmin=2
};

I would later like to make decisions based on the enum type setting:

Exclusions exclude = Exclusions.HaveEmail & Exclusions.IsAdmin;

switch(exclude)
{
case Exclusions.None:
//dosomething
break;
case Exclusions.HaveEmail:
//dosomething
break;
case Exclusions.IsAdmin:
//dosomething
break;
case (Exclusions.HaveEmail & Exclusions.IsAdmin): //***ERROR***
//dosomething
break;
}

When I try to handle the case where exclude=3 (HaveEmail & IsAdmin), the
compiler generates an error saying the case is already hadled.

Can someone please illuminate why this is not allowed and tell me if it's
possible to do this without puting numeric constants in my case statement?

Thank you very much,
-Keith

Nov 16 '05 #1
2 10623
Hi Keith,

You need to use bitwise OR rather then AND. with AND it will always give you
0. as long as your flags are only one bit.

The bottom line if you want to combine flags use | operator

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Keith Harris" <Ke*********@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
Hi,

To exclude users based on a criteria, I have created an enum like so:

[FlagsAttribute]
public enum Exclusions:byte
{
None=0,
HaveEmail=1,
IsAdmin=2
};

I would later like to make decisions based on the enum type setting:

Exclusions exclude = Exclusions.HaveEmail & Exclusions.IsAdmin;

switch(exclude)
{
case Exclusions.None:
//dosomething
break;
case Exclusions.HaveEmail:
//dosomething
break;
case Exclusions.IsAdmin:
//dosomething
break;
case (Exclusions.HaveEmail & Exclusions.IsAdmin): //***ERROR***
//dosomething
break;
}

When I try to handle the case where exclude=3 (HaveEmail & IsAdmin), the
compiler generates an error saying the case is already hadled.

Can someone please illuminate why this is not allowed and tell me if it's
possible to do this without puting numeric constants in my case statement?

Thank you very much,
-Keith

Nov 16 '05 #2
Keith Harris <Ke*********@discussions.microsoft.com> wrote in message news:<2D**********************************@microso ft.com>...
Hi,

To exclude users based on a criteria, I have created an enum like so:

[FlagsAttribute]
public enum Exclusions:byte
{
None=0,
HaveEmail=1,
IsAdmin=2
};

I would later like to make decisions based on the enum type setting:

Exclusions exclude = Exclusions.HaveEmail & Exclusions.IsAdmin;

switch(exclude)
{
case Exclusions.None:
//dosomething
break;
case Exclusions.HaveEmail:
//dosomething
break;
case Exclusions.IsAdmin:
//dosomething
break;
case (Exclusions.HaveEmail & Exclusions.IsAdmin): //***ERROR***
//dosomething
break;
}

When I try to handle the case where exclude=3 (HaveEmail & IsAdmin), the
compiler generates an error saying the case is already hadled.

Can someone please illuminate why this is not allowed and tell me if it's
possible to do this without puting numeric constants in my case statement?

Thank you very much,
-Keith


Remember you are using and....but you two flags are in binary 01 and
10....if you & them thats 0....you say you want 3...you need to
|...which gives 11 or 3
Nov 16 '05 #3

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

Similar topics

9
by: AngleWyrm | last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T, section 4.8 (pp 77): "A value of integral type may be explicitly converted to an enumeration type. The result of such a...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
3
by: Matt | last post by:
Hi, Recently we had some code like this cause a failure: MyEnum myEnum = (MyEnum) (int) dt; i.e. reading an int out of the database and casting it into a type-safe enum. The thought...
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,...
16
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
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...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
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...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.