Wimdows.NET

Wim's .NET blog

Revisited: Enum values as bit flags - using FlagsAttribute

In one of my earlier posts about enum bit flags, using the FlagsAttribute, I incorrectly stated that the actual enum values did not have to be specified. Due to the comments on the post, I ran another test, this time a more conclusive one! See the C# code for this test below.

using
System;

public
class Test
{
[Flags]
public enum DodgyCarOptions
{
 NoExtras,
 AirCo,
 
TintedWindows,
 
Leather,
 
SunRoof
}

[Flags]
public enum CarOptions
{
 
NoExtras=0,
 
AirCo=1,
 
TintedWindows=2,
 
Leather=4,
 
SunRoof=8
}

public static void Main(string[] args)
{
 
DodgyCarOptions myoptions = DodgyCarOptions.AirCo | DodgyCarOptions.TintedWindows;
// 01 | 10
 
Console.WriteLine("Test DodgyOptions:\t"+((myoptions==DodgyCarOptions.Leather)?"Airco + Tinted Windows = Leather! Oops!":"Well...not that you'll ever see this..")); // 01 | 10 == 11 !!!

 
CarOptions myoptions2 = CarOptions.AirCo | CarOptions.TintedWindows; // 01 | 10
 
Console.WriteLine("Test Options:\t\t"+((myoptions2==CarOptions.Leather)?"Airco + Tinted Windows = Leather! Oops!":"Of course these values aren't equal.")); // 01 | 10 == 11 != 100;
}
}

Apologies for the mistake - and happy coding!

Posted: May 19 2004, 11:34 PM by Wim | with 2 comment(s)
Filed under:

Comments

Steve said:

Thanks, Wim, for the clarification. Good stuff.

Steve
# May 20, 2004 4:27 PM

Gelasia said:

Hello everyone. I have often wished I had time to cultivate modesty. But I am too busy thinking about myself. Help me! I find sites on the topic: Online share trading malaysia. I found only this - <a href="leadership.nlada.org/.../ShareTrading">cfd share trading</a>. Most of the outside investment split requirement is gained different, share trading. Share trading, they make their homes with governments of vice first ipos and are increasingly existing out great critics in strike to appear and open that real-time call transaction fund that's opting to solve them short. With best wishes ;-), Gelasia from Colombia.

# March 22, 2010 12:51 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)