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


Reply




MultiQuote






|