A number of times I find my self looking up how to assign multiple enum values to a property to support options semantics on a control. So to make it easier to find I am TidByteing it here. An example of this is the classic Border property that allows you to identify where you want lines drawn. For example, lets say we have an enum that looks like this (note that I am using powers of two so they can easily be combined):
public enum Border
{
Top = 1,
Bottom = 2,
Left = 4,
Right = 8
}
And you want to indicate to draw only on the left and right borders would look something like this:
ctl.Border = Border.Left | Border.Right
To make this work you need to add the FlagsAttribute to the enum:
Then to check to see if a flag is set you would do the following:
if ((propertyName & Border.Left) == Border.Left)
//border left border needs to be drawn;
Posted in Code | TidByte |Comments [0]
Sysknowlogy