473,387 Members | 3,750 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,387 software developers and data experts.

How to test if an enum is inside a set

Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I'm looking for something like this in C#.
I can't find it. Anybody has found it already?

Kind regards

Alexander
Nov 16 '05 #1
20 3456
Alexander,

If you are looking to see if the value is in a subset of the values in
an enumeration, then you will have to do it the way you are doing it (or
place the subset in an array and then search through that).

However, if the set you are looking through is the enumeration yourself,
then you can call the static IsDefined method on the Enum class to determine
whether or not the value is in the enumeration.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexander Muylaert" <Re********@pandora.be> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I'm looking for something like this in C#.
I can't find it. Anybody has found it already?

Kind regards

Alexander

Nov 16 '05 #2
Put the FlagsAttribute thingy on the enum, and then have
if((aEnum & (Green | Red | Yellow)) != 0)
....

have to make sure the values of the enum are like 0x01, 0x02, 0x10, 0x20,
etc.

"Alexander Muylaert" <Re********@pandora.be> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I'm looking for something like this in C#.
I can't find it. Anybody has found it already?

Kind regards

Alexander

Nov 16 '05 #3
Alexander,

There's no direct equivalent. However, something like the following should
make things a bit more convenient:

public bool EnumInList(Enum target, params Enum[] allowedValues)
{
bool found = false;

for (int i = 0; i < allowedValues.Length; i++)
{
if (Enum.Equals(allowedValues[i], target))
{
found = true;
break;
}
}

return found;
}

For better performance, use your particular enum type rather than the Enum
base class.

HTH,
Nicole
"Alexander Muylaert" <Re********@pandora.be> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I'm looking for something like this in C#.
I can't find it. Anybody has found it already?

Kind regards

Alexander

Nov 16 '05 #4
IsDefined isn't really a great choice even the desired set covers the enum
values. See http://blogs.msdn.com/brada/archive/.../29/50903.aspx for
details.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eL**************@tk2msftngp13.phx.gbl...
Alexander,

If you are looking to see if the value is in a subset of the values in
an enumeration, then you will have to do it the way you are doing it (or
place the subset in an array and then search through that).

However, if the set you are looking through is the enumeration
yourself,
then you can call the static IsDefined method on the Enum class to
determine
whether or not the value is in the enumeration.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexander Muylaert" <Re********@pandora.be> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I'm looking for something like this in C#.
I can't find it. Anybody has found it already?

Kind regards

Alexander


Nov 16 '05 #5
Thanks a lot

This is a really, really usefull feature of delphi. I can't figuere out why
they didn't implemented the set behaviour of Delphi in C#.

Kind regards

Alexander
Nov 16 '05 #6
Alexander Muylaert wrote:
This is a really, really usefull feature of delphi. I can't figuere out why
they didn't implemented the set behaviour of Delphi in C#.


Here's one theory:

include delphi-like sets -->
alienate current c++ users -->
less c++ users switch over to c# -->
microsoft lose profit
Nov 16 '05 #7

----- Alexander Muylaert wrote: -----
Thanks a lot This is a really, really usefull feature of delphi. I can't figuere out why
they didn't implemented the set behaviour of Delphi in C#.


because C# is based on C family syntax, not Delphi syntax I guess.
Nov 16 '05 #8
A few reasons:
- Back in the old Pascal days, sets used to be very slow; Although
processors have become much faster, and compilers a lot better, I still
think using them in statements like "if x in [1,2,3]" really hurts
performance
- In your case, using a flag-enum would do exactly the same job (but much
faster)
- The .net framework provides a the Hashtable class that's more flexible
than delphi-sets, as any class may be used as key
- C# - as the name implies - is a relative of C and C++; neither of them
have sets
- Usually, looking at a syntax element should tell you roughly how expensive
it is; Noone expects that "[1]" internally creates an object that 256 bytes
big! (assuming delphi sets are more or less the same as old turbo pascal
sets)

Niki

"Alexander Muylaert" <Re********@pandora.be> wrote in
news:eO**************@TK2MSFTNGP10.phx.gbl...
Thanks a lot

This is a really, really usefull feature of delphi. I can't figuere out why they didn't implemented the set behaviour of Delphi in C#.

Kind regards

Alexander

Nov 16 '05 #9

"C# Learner" <cs****@learner.here> wrote in message
news:eE**************@TK2MSFTNGP11.phx.gbl...
Alexander Muylaert wrote:
This is a really, really usefull feature of delphi. I can't figuere out
why
they didn't implemented the set behaviour of Delphi in C#.


Here's one theory:

include delphi-like sets -->
alienate current c++ users -->
less c++ users switch over to c# -->
microsoft lose profit


LOL, it is a nice concept however, so I would hope Microsoft would be more
open minded than that. I'd like to see an isin(it makes more sense than just
in, however in or a spaced keyword is in could be used to spare a keyword)
operator that works across IList, ICollection<T>, arrays, strings, and a few
other bits(wrapper around .Contains for various types mostly). I've been
considering implementing it in Mono just to see how it works, but I havn't
had time(as an offisde, I did implement it in another language and the
concept worked fairly clearly, IMHO). Its easy to declare inline arrays so
that isn't an issue, its just a rather simple keyword. Its mostly usability
and cleanliness reasons to consider, the biggest problem is that it doesn't
operate over one given interface like most keywords do, instead it has to
operate over several(IList, ICollection<T>, and string primarily, though for
the sake of a more functional use it may be appropriate for it to work over
IEnumerable\IEnumerator implementations as well, if none of the other
interfaces exist)
Nov 16 '05 #10
using set in Delphi doesn't hurt performance nothing more then

the "in" statement in delphi does nothing more then if ((X & Mask) != 0) .
Since it uses 32-bit arithmetics and AND of the cpu it is not slow. Not
slower then and anyway ;-)

C# is mainly invented by the guy that gave us Delphi. C# actually has more
in common with Delphi and Java than it has in common with C or C++. Except
for the syntax, wich is of no importance.

Kind regards

Alexander

Nov 16 '05 #11
Daniel Jin wrote:
except Anders Hejlsberg (and Microsoft) obviously decided, like java, to model the syntax after C family instead of Pascal/Delphi family.


That's hardly a valid argument here. The people behind C# could've
picked constructs from _any_ language and applied them to C#.

And they _did_ do this, for example with properties. Neither C nor C++
has properties.
Nov 16 '05 #12
>btw, isn't there something called Delphi.NET available?

If you cut of a chickes head, it will continue walking for a while, but it
will never lay an egg anymore.

That is more or less what happened with Delphi after D4. It kinda never
evolved. Octane is pretty, but it is only pretty because it integrates
..net. It took ms 10 years to catch up with Delphi, but they did one heck of
a job.

But times change and after testing Octane I don't see any advantage over C#.
C# has some very nice features and it is way, way cheaper then Delphi.
($1000 vs $3500). I have to say to that to use a tiny class from Delphi
inside C# you have to include 7000 assemblies with your installation. This
kinda removes all the advantages of 1 framework.

Kind regards

Alexander
Nov 16 '05 #13
That's the problem - delphi's just a bit too "twee" for most people.

Nov 16 '05 #14

"Beeeeves" <an*******@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
That's the problem - delphi's just a bit too "twee" for most people.


LOL, twee huh? Wonder how many non-brits know what it means, I had to look
it up.

The problem with delphi has nothign to do with being "twee", its the syntax
that kills it. I would say delphi(and pascal) are probably at the bottom of
my list as far as syntax goes. I'd rather use lisp.

However, I don't see how your response relates to the post in any way, could
you perhaps have quoted so it made sense?
Nov 16 '05 #15
What I was on about it being "twee" is exactly as you said - it has
crap syntax (not a single bracket in sight - that can't be good for
the compiler). IOW, the inventor(s) seem(s) to have pandered
to the whims of users who want to write "nice" looking code,
not how it is designed to be easy for the compiler (to optimize)
aswell as to code.
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ug**************@tk2msftngp13.phx.gbl...

"Beeeeves" <an*******@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
That's the problem - delphi's just a bit too "twee" for most people.
LOL, twee huh? Wonder how many non-brits know what it means, I had to look
it up.

The problem with delphi has nothign to do with being "twee", its the

syntax that kills it. I would say delphi(and pascal) are probably at the bottom of my list as far as syntax goes. I'd rather use lisp.

However, I don't see how your response relates to the post in any way, could you perhaps have quoted so it made sense?

Nov 16 '05 #16

"Beeeeeves" <1234512345789123456789> wrote in message
news:e2***************@TK2MSFTNGP10.phx.gbl...
What I was on about it being "twee" is exactly as you said - it has
crap syntax (not a single bracket in sight - that can't be good for
the compiler). IOW, the inventor(s) seem(s) to have pandered
to the whims of users who want to write "nice" looking code,
not how it is designed to be easy for the compiler (to optimize)
aswell as to code.
Hrmm, I don't consider it particluarly nice looking, so I guess thats where
you lost me.

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ug**************@tk2msftngp13.phx.gbl...

"Beeeeves" <an*******@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
> That's the problem - delphi's just a bit too "twee" for most people.


LOL, twee huh? Wonder how many non-brits know what it means, I had to
look
it up.

The problem with delphi has nothign to do with being "twee", its the

syntax
that kills it. I would say delphi(and pascal) are probably at the bottom

of
my list as far as syntax goes. I'd rather use lisp.

However, I don't see how your response relates to the post in any way,

could
you perhaps have quoted so it made sense?


Nov 16 '05 #17
Daniel O'Connell [C# MVP] wrote:

[...]
The problem with delphi has nothign to do with being "twee", its the syntax
that kills it. I would say delphi(and pascal) are probably at the bottom of
my list as far as syntax goes. [...]


The first time I saw Delphi/Pascal syntax, I cringed. I hated it
straight away.

I thought I'd try Delphi anyway, since I'd heard great things about the
environment. I ended up loving the language, as I find it to be so nice
and clear.

With C and me, it goes the other way. At first, I loved C's
minimalistic syntax. Everything about it seemed perfect. After using
it for some time, though, I ended up not liking it so much.

It's a case of different people and different tastes, I guess.
Nov 16 '05 #18
Beeeeeves wrote:
What I was on about it being "twee" is exactly as you said - it has
crap syntax (not a single bracket in sight - that can't be good for
the compiler).
Whereas, on the other hand, I don't really like C's braces.

Even though 'begin' and 'end' are too verbose, IMO, I'd much rather have
block specifiers that consist of multiple characters. It makes for more
readable code, IMO.
[...]

Nov 16 '05 #19
> IOW, the inventor(s) seem(s) to have pandered
to the whims of users who want to write "nice" looking code,
not how it is designed to be easy for the compiler (to optimize)
aswell as to code.


Euh, you're joking right? What is the difference of "begin" or "{" as token
for the optimizer?

begin
DoThing();
end;

{
DoThing();
}

Honestly, if you believe that the first syntax is less optimizable then the
second, then I can advise you a great book of Steven S. Muchnick. Advanced
compiler design. Perhaps the parses looses some ticks because the tokens in
pascal are multicharacter. But, trust me, your cpu doesn't care if you
assigned with a := or with a =. MOV is MOV and that's it.

btw, are you aware that Delphi is a 1 pass compiler where as C/C++ are two
pass compilers.

It is true, that delphi doesn't allow you to write something like this

if (MyTempVar = (++Counter++ == 5)) {
Counter += ++MyTempVar-- != 2;
}

Perfectly valid syntax in C/C++. But I think it is safely to say that even
a C-die-hard manager would fire any guy that codes this way. I like pascal
since it forces you to use multiple statements. It keeps code easier to
track.

But, apart from the syntax. Delphi has some really nice features I miss in
C# and vica versa.

Kind regards

Alexander
Nov 16 '05 #20
> if (MyTempVar = (++Counter++ == 5)) {
Counter += ++MyTempVar-- != 2;
}


I would also like to add that the C piece of code is *not* faster because it
is on 2 lines.

in pascal we would write it this way.

Inc(Counter);
MyTempVar := Ord(Counter = 5);
Inc(Counter);
if True then begin
Inc(MyTempVar);
Counter := Counter + Ord(MyTempVar <> 2);
Dec(Counter);
end;

Inc, Dec and Ord are *not* functions, it is compilermagic.

Compile both and check out the assembler. It is the same :-)

Amazing? And this even works on german and french computers ;-)

Kind regards

Alexander
Nov 16 '05 #21

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

Similar topics

11
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't...
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
2
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product...
21
by: dllhell | last post by:
hi all, I have a problem with creating proc with a enum as a param. I wish to pass an enumeration in proc in which one I intend to do some operations with enumeration, but I don't know which one...
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: tillhm | last post by:
Hello, I have an enum in one of my classes class Config { public: SquareData bg; //background ............ The enum looks like this enum SquareData{
4
by: ice8595 | last post by:
Hi there, I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated. Essentially I'm...
2
by: NullQwerty | last post by:
Hey folks, So, I've got three enum types: enum enum1 enum enum2 enum enum3 And then I've got a function overloaded three times to accept each enum type: private void func(enum1 myenum){}
8
by: benn | last post by:
Here's the setup... Defines.h file contains: enum DAY { monday, tueday }; DayFunctions.h contains prototype: void printIsMonday ( enum DAY currentDay); DayFunctions.c contains:
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: 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
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
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...
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
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
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...

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.