Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 86,265 C# Programmers. There are 1,895 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C# Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

C# Flags

 
Reply to this topicStart new topic

> C# Flags, Howto use flags to create settings

sontek
Group Icon



post 4 Apr, 2006 - 04:42 AM
Post #1


Using the [Flags] attribute on an enum allows to to perform bitwise operations on it so you can use to have settings for class.

This is especially usefull when a class can have multiple settings applied to it.

The example i'm using here is a Font, which can be Bold, Italic, or Underlined. So you'll notice I setup an enumeration with that special flags attribute.. One other thing you have to do is make the enumeration in powers of 2 (Because if you don't you'll do something like 1 | 2 == 3 which is another property of the Enum)

I hope this code helps ya guys out!
CODE

using System;
using System.Collections.Generic;
using System.Text;

namespace TestFlags
{
   class Program
   {
       static void Main(string[] args)
       {
           Font font = new Font();
           font.FontProps = FontProperties.Bold | FontProperties.Italic;

           Console.WriteLine(font.FontProps);
           Console.ReadLine();
       }
   }
   [Flags]
   public enum FontProperties
   {
       Bold = 1,
       Italic = 2,
       Underlined = 4,
       None = 8
   }
   public class Font
   {
       FontProperties fontProps;
       internal FontProperties FontProps
       {
           get { return fontProps; }
           set { fontProps = value; }
       }
       public Font()
       {
           fontProps = FontProperties.None;
       }
   }
}



Register to Make This Ad Go Away!

graeme bradbury
*



post 9 Aug, 2007 - 02:31 AM
Post #2
It's rather bad practice to to have the "None" element of a flag enum to be a value other than 0.

This is because the literal constant 0 implicitly converts to any enum type and means "all bits zero"

So the properties enum is better being
CODE

[Flags]
public enum FontProperties
{
  None = 0,
  Bold = 1,
  Italic = 2,
  Underlined = 4
}


Something to also consider when using flags is to provide special enums for commonly used combinations. So in this example fonts are often bold and italic so the enum could look like
CODE

[Flags]
public enum FontProperties
{
  None = 0,
  Bold = 1,
  Italic = 2,
  BoldItalic = Bold | Italic,
  Underlined = 4
}


This post has been edited by graeme bradbury: 9 Aug, 2007 - 02:32 AM


Fast ReplyReply to this topicStart new topic
3 User(s) are reading this topic (3 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 5/16/08 10:24AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month