PC Review


Reply
Thread Tools Rate Thread

Or'ing enums together

 
 
Tim Johnson
Guest
Posts: n/a
 
      17th Mar 2005
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



 
Reply With Quote
 
 
 
 
Daniel Moth
Guest
Posts: n/a
 
      17th Mar 2005
Look up the Flags attribute.

There is also this article (scroll down to "Bit Flag Types"):
http://msdn.microsoft.com/msdnmag/issues/01/10/net/

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


"Tim Johnson" <(E-Mail Removed)> wrote in message
news:OvtFX$(E-Mail Removed)...
> 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
>
>
>


 
Reply With Quote
 
Tim Johnson
Guest
Posts: n/a
 
      17th Mar 2005
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


"Daniel Moth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Look up the Flags attribute.
>
> There is also this article (scroll down to "Bit Flag Types"):
> http://msdn.microsoft.com/msdnmag/issues/01/10/net/
>
> Cheers
> Daniel
> --
> http://www.danielmoth.com/Blog/
>
>
> "Tim Johnson" <(E-Mail Removed)> wrote in message
> news:OvtFX$(E-Mail Removed)...
>> 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
>>
>>
>>

>



 
Reply With Quote
 
Tim Johnson
Guest
Posts: n/a
 
      17th Mar 2005
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


"Daniel Moth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Look up the Flags attribute.
>
> There is also this article (scroll down to "Bit Flag Types"):
> http://msdn.microsoft.com/msdnmag/issues/01/10/net/
>
> Cheers
> Daniel
> --
> http://www.danielmoth.com/Blog/
>
>
> "Tim Johnson" <(E-Mail Removed)> wrote in message
> news:OvtFX$(E-Mail Removed)...
>> 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
>>
>>
>>

>



 
Reply With Quote
 
Chris Tacke, eMVP
Guest
Posts: n/a
 
      17th Mar 2005
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


"Tim Johnson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> "Daniel Moth" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Look up the Flags attribute.
>>
>> There is also this article (scroll down to "Bit Flag Types"):
>> http://msdn.microsoft.com/msdnmag/issues/01/10/net/
>>
>> Cheers
>> Daniel
>> --
>> http://www.danielmoth.com/Blog/
>>
>>
>> "Tim Johnson" <(E-Mail Removed)> wrote in message
>> news:OvtFX$(E-Mail Removed)...
>>> 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
>>>
>>>
>>>

>>

>
>



 
Reply With Quote
 
Tim Johnson
Guest
Posts: n/a
 
      17th Mar 2005
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


"Chris Tacke, eMVP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> "Tim Johnson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>
>> "Daniel Moth" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Look up the Flags attribute.
>>>
>>> There is also this article (scroll down to "Bit Flag Types"):
>>> http://msdn.microsoft.com/msdnmag/issues/01/10/net/
>>>
>>> Cheers
>>> Daniel
>>> --
>>> http://www.danielmoth.com/Blog/
>>>
>>>
>>> "Tim Johnson" <(E-Mail Removed)> wrote in message
>>> news:OvtFX$(E-Mail Removed)...
>>>> 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
>>>>
>>>>
>>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      17th Mar 2005
Strictly speaking it should be
if((x & MyEnum.Test1) == MyEnum.Test1)

--
Alex Feinman
---
Visit http://www.opennetcf.org
"Chris Tacke, eMVP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> "Tim Johnson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>
>> "Daniel Moth" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Look up the Flags attribute.
>>>
>>> There is also this article (scroll down to "Bit Flag Types"):
>>> http://msdn.microsoft.com/msdnmag/issues/01/10/net/
>>>
>>> Cheers
>>> Daniel
>>> --
>>> http://www.danielmoth.com/Blog/
>>>
>>>
>>> "Tim Johnson" <(E-Mail Removed)> wrote in message
>>> news:OvtFX$(E-Mail Removed)...
>>>> 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
>>>>
>>>>
>>>>
>>>

>>
>>

>
>


 
Reply With Quote
 
Chris Tacke, eMVP
Guest
Posts: n/a
 
      17th Mar 2005
Depends on what you're checking for.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


"Alex Feinman [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Strictly speaking it should be
> if((x & MyEnum.Test1) == MyEnum.Test1)
>
> --
> Alex Feinman
> ---
> Visit http://www.opennetcf.org
> "Chris Tacke, eMVP" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>
>> "Tim Johnson" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>>>
>>>
>>> "Daniel Moth" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> Look up the Flags attribute.
>>>>
>>>> There is also this article (scroll down to "Bit Flag Types"):
>>>> http://msdn.microsoft.com/msdnmag/issues/01/10/net/
>>>>
>>>> Cheers
>>>> Daniel
>>>> --
>>>> http://www.danielmoth.com/Blog/
>>>>
>>>>
>>>> "Tim Johnson" <(E-Mail Removed)> wrote in message
>>>> news:OvtFX$(E-Mail Removed)...
>>>>> 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
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>>

>>
>>

>



 
Reply With Quote
 
Chris Tacke, eMVP
Guest
Posts: n/a
 
      17th Mar 2005
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


"Tim Johnson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> "Chris Tacke, eMVP" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>
>> "Tim Johnson" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>>>
>>>
>>> "Daniel Moth" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> Look up the Flags attribute.
>>>>
>>>> There is also this article (scroll down to "Bit Flag Types"):
>>>> http://msdn.microsoft.com/msdnmag/issues/01/10/net/
>>>>
>>>> Cheers
>>>> Daniel
>>>> --
>>>> http://www.danielmoth.com/Blog/
>>>>
>>>>
>>>> "Tim Johnson" <(E-Mail Removed)> wrote in message
>>>> news:OvtFX$(E-Mail Removed)...
>>>>> 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
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Tim Johnson
Guest
Posts: n/a
 
      18th Mar 2005
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


"Tim Johnson" <(E-Mail Removed)> wrote in message
news:OvtFX$(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with enums nomad Microsoft C# .NET 4 12th Jun 2008 03:57 PM
Enums and Arrays of Enums J055 Microsoft C# .NET 3 13th Sep 2006 12:31 AM
xsd.exe and Enums robert madrian Microsoft Dot NET Framework 0 27th Jul 2006 06:20 PM
"global" enums vs. enums in class? Harold Hsu Microsoft VB .NET 1 21st Oct 2004 08:40 PM
enums martin Microsoft ASP .NET 2 28th May 2004 06:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:43 PM.