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

Checking if enum type value contains certain value

Hello,

Is there some short-gand way to check if the enum type value contains
certain value?

Ie, the talk here is about Regex object:

Regex re = new Regex(@"pattern", RegexOptions.IgnoreCase |
RegexOptions.Multiline);
if ((re.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) ...
do stuff ...

This "if" expression is rather lengthy, perhaps there is some shorter way to
check wether the value contains one of the ORed enum values?

Thanks,

Pavils
Nov 16 '05 #1
2 3408
Pavils Jurjans <pa****@mailbox.riga.lv> wrote:
Is there some short-gand way to check if the enum type value contains
certain value?

Ie, the talk here is about Regex object:

Regex re = new Regex(@"pattern", RegexOptions.IgnoreCase |
RegexOptions.Multiline);
if ((re.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) ...
do stuff ...

This "if" expression is rather lengthy, perhaps there is some shorter way to
check wether the value contains one of the ORed enum values?


Well, you don't need to check for equality - just "non-zeroness":

if ((re.Options & RegexOptions.IgnoreCase) != 0)

which is fairly straightforward.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Yes, that's true!

I was not aware that runtime will implicitly convert enum type to int so
that it can be compared to zero.

Thanks!

Pavils.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pavils Jurjans <pa****@mailbox.riga.lv> wrote:
Is there some short-gand way to check if the enum type value contains
certain value?

Ie, the talk here is about Regex object:

Regex re = new Regex(@"pattern", RegexOptions.IgnoreCase |
RegexOptions.Multiline);
if ((re.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) .... do stuff ...

This "if" expression is rather lengthy, perhaps there is some shorter way to check wether the value contains one of the ORed enum values?


Well, you don't need to check for equality - just "non-zeroness":

if ((re.Options & RegexOptions.IgnoreCase) != 0)

which is fairly straightforward.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

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...
2
by: MT | last post by:
Hi, I am currently validating an XML file against a Schema using XMLValidatingReader. The schema actually contains ranges for particular elements and I have been using it to detect range errors...
2
by: mrhicks | last post by:
Hello all, I have a question about enumerations. Within some requirements data passed back a certain bit field is defined by three bits then in another section the bit field is defined as 4...
18
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
1
by: Pavils Jurjans | last post by:
Hello, Is there some short-gand way to check if the enum type value contains certain value? Ie, the talk here is about Regex object: Regex re = new Regex(@"pattern", RegexOptions.IgnoreCase...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
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...
5
by: Sin Jeong-hun | last post by:
Hello. This question may be stupid. enum MyValues { Value1, Value2, Value3, Value4, Value5 } ....in the code...
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?
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
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...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.