in set operator for enumerated types?

S

Simon Storr

Posting this again as it just got deleted off the server?!

Is it possible to perform an 'in' set operator check on an enumerated type?
For example something like

enum WeekDays = {Mon,Tue,Wed,Thu,Fri,Sat,Sun};
WeekDays aWeekDay;
..
..
..
if (aWeekDay in {Sat,Sun}) // <-- can't find a way of doing this?
{
HurrahItsTheWeekend();
}

rather than

if ((aWeekDay == Sat)||(aWeekDay == Sun))
{
HurrahItsTheWeekend();
}

Can't find anything on set operators for C# :blush:(

TIA!

Simon
 
J

Jon Skeet

Simon Storr said:
Posting this again as it just got deleted off the server?!

No, you just didn't wait long enough.
Is it possible to perform an 'in' set operator check on an enumerated type?
For example something like

enum WeekDays = {Mon,Tue,Wed,Thu,Fri,Sat,Sun};
WeekDays aWeekDay;
.
.
.
if (aWeekDay in {Sat,Sun}) // <-- can't find a way of doing this?
{
HurrahItsTheWeekend();
}

rather than

if ((aWeekDay == Sat)||(aWeekDay == Sun))
{
HurrahItsTheWeekend();
}

Can't find anything on set operators for C# :blush:(

You could create a list or an array of the appropriate type, and use
IndexOf - that's the easiest way I can think of, to be honest. There's
no particularly clean syntax for it - I don't think your above
comparison would even work, as it would require WeekDays.Sat and
WeekDays.Sun.
 
S

Simon Storr

Its weird - it did briefly appear then disappeared when I clicked on it -
maybe a prob with my newsreader. Never had it happen before. Apologies if
peeps are seeing multiple posts (my repost seems to have stayed put though
:blush:)

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top