473,405 Members | 2,262 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,405 software developers and data experts.

enums

Below is code for testing whether an enum is part of
a "set" of enums. There must be some better way to do
this. How does one get a group of enums and test for
inclusion? I want to use sets. But maybe collections?
What is the right way to proceed?
enum Suit: int {spade=1, club=2, heart=4,
diamong=8};

private void button1_Click(object sender,
System.EventArgs e)
{
Suit blackSuits = Suit.spade |
Suit.club;
if ((blackSuits & Suit.club) != 0)
MessageBox.Show
(this, "Yes");
else
MessageBox.Show
(this, "No");
}

Nov 15 '05 #1
4 1767
Didn't know it would format like that. Here is the example in readable form:

enum Suit: int {spade=1, club=2, heart=4, diamong=8};

private void button1_Click(object sender, System.EventArgs e)
{
Suit blackSuits = Suit.spade | Suit.club;
if ((blackSuits & Suit.club) != 0)
MessageBox.Show(this, "Yes");
else
MessageBox.Show(this, "No");
}

How is one supposed to do this sort of thing? Having to declare the enum
with values such as 1, 2, 4 and 8 is a terrible hack. How am I supposed
to work with enums in C#?

- Charlie

Nov 15 '05 #2
Well, I disagree that it's a 'hack' to declare enum values
so that they are bitwise comparable, but that's your
choice to make. This is why the FlagAttribute exists - to
indicate to consumers that an enum is intended for flag or
bitwise comparison.

It's fast and while not intuitively human-readable,
bitwise comparisons are well known constructs, so that
shouldn't be a problem.

Richard
-----Original Message-----
Didn't know it would format like that. Here is the example in readable form:
enum Suit: int {spade=1, club=2, heart=4, diamong=8};

private void button1_Click(object sender, System.EventArgs e){
Suit blackSuits = Suit.spade | Suit.club;
if ((blackSuits & Suit.club) != 0)
MessageBox.Show(this, "Yes");
else
MessageBox.Show(this, "No"); }

How is one supposed to do this sort of thing? Having to declare the enumwith values such as 1, 2, 4 and 8 is a terrible hack. How am I supposedto work with enums in C#?

- Charlie

.

Nov 15 '05 #3
My apologies for my choice of words. I should be less outspoken. All I
really care about is doing things the "proper way" as defined by common
C# usage, and if this is the way to go, then that is fine with me. I get
used to doing things one way in one language, and I should know enough
to keep an open mind when seeing how things are done elsewhere.
Certainly after working in Java for a bit, I should be grateful that C#
has enums!

- Charlie

Richard A. Lowe wrote:
Well, I disagree that it's a 'hack' to declare enum values
so that they are bitwise comparable, but that's your
choice to make. This is why the FlagAttribute exists - to

Richard

-----Original Message-----
Didn't know it would format like that. Here is the


example in readable form:
enum Suit: int {spade=1, club=2, heart=4, diamong=8};

private void button1_Click(object sender,


System.EventArgs e)
{
Suit blackSuits = Suit.spade | Suit.club;
if ((blackSuits & Suit.club) != 0)
MessageBox.Show(this, "Yes");
else
MessageBox.Show(this, "No");

}


Nov 15 '05 #4
Have you had a look to System.Enum.Parse()?

José

"Charlie" <ch************@msn.com> a écrit dans le message de
news:ad****************************@phx.gbl...
Below is code for testing whether an enum is part of
a "set" of enums. There must be some better way to do
this. How does one get a group of enums and test for
inclusion? I want to use sets. But maybe collections?
What is the right way to proceed?
enum Suit: int {spade=1, club=2, heart=4,
diamong=8};

private void button1_Click(object sender,
System.EventArgs e)
{
Suit blackSuits = Suit.spade |
Suit.club;
if ((blackSuits & Suit.club) != 0)
MessageBox.Show
(this, "Yes");
else
MessageBox.Show
(this, "No");
}

Nov 15 '05 #5

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

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
4
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
13
by: Bob | last post by:
Hi, Can someone explain why you can't declare enums in an interface? The compiler says "interfaces can't declare types" Ignoring the syntax implications it seems to me that you should be able to...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.