Or'ing enums together

  • Thread starter Thread starter Tim Johnson
  • Start date Start date
T

Tim Johnson

Is there an approved .net way to define or-able enums? I'd like to define
enum items with values of 1,2,4,8, etc and be able to OR them together as a
mask as follows:

public enum Tests
{
Test1=1,
Teste2=2,
Test3=4,
Test4=8,
Test5=16
}

Tests teststodo = Test1 | Test3 | Test4;
PerformTests(teststodo);

private void PerformTests(Tests enumMask)
{
}

But in that example "teststodo" does not contain a legitimate enum value
from the predefined list. How do you do that?

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
Perfect, thanks! Oddly it works without the Flags attribute, although as
the article points out you don't get a good string from ToString. Putting
in [Flags] makes ToString return a nice comma-separated string of all or'd
values in the mask.

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
However, slight problem -

I can OR two enums together as in

MyEnum x = MyEnum.Test1 | MyEnum.Test2;

But I get a compile error when trying to test it with:

if (x & MyEnum.Test1)
DoTest1();

The compiler says "Cannot implicitly convert type MyEnum to bool". Do you
know what the problem is? I've tried several explicit casts and alternate
types with no success. The enum itself is not typed so I think it defaults
to Int32.

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
It's telling you the problem (and I too dislike it).

if() requires a bool.
x & MyEnum.Test1 returns a MyEnum

do this:

if((x & MyEnum.Test1) > 0)


--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
Yup, that did it. Thanks. You'd think the compiler could figure this out
implicitly though by context.

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
You'd think the compiler wouldn't complain about this either, but it does,
and again it irritates me:

byte a = 0x01;
byte b = 0x02;

byte c = a | b;

You have to do this:

byte c = (byte)(a | b);

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
Ok bitheads - once I test the flag and execute my routine, I want that
routine to clear the flag. But I can't do what I'd expect. So to try to
clear the myFlags mask of just the Test1 flag I can't do this:

myFlags &= !Test1;

I can however do this:

myFlags-= Test1;

but that sort of goes against the grain of using "flags" that are OR'd and
AND'd (set and cleared). Any suggestions?
--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
Consider using ~ instead of !.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
That did it, thanks. I have too many language syntaxes floating around in
my head after all these years!

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625


"Ilya Tumanov [MS]" said:
Consider using ~ instead of !.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Tim Johnson" <[email protected]>
References: <[email protected]>
Subject: Re: Or'ing enums together
Date: Fri, 18 Mar 2005 11:50:12 -0800
Lines: 56
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Response
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: 70-58-192-81.ptld.qwest.net 70.58.192.81
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.compactframework:73597
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

Ok bitheads - once I test the flag and execute my routine, I want that
routine to clear the flag. But I can't do what I'd expect. So to try to
clear the myFlags mask of just the Test1 flag I can't do this:

myFlags &= !Test1;

I can however do this:

myFlags-= Test1;

but that sort of goes against the grain of using "flags" that are OR'd and
AND'd (set and cleared). Any suggestions?
--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
Back
Top