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

Join 119,058 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,526 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



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;
       }
   }
}

Go to the top of the page
+Quote Post


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
Go to the top of the page
+Quote Post


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

 

Lo-Fi Version Time is now: 10/13/08 03:42PM

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