Enums and Text Descriptions

  • Thread starter Thread starter Kyle Novak
  • Start date Start date
K

Kyle Novak

What I want to do is take the values of an enum and have a related textual
description for each item. For example, if I have an enum declared as the
following...

[Flags]
public enum EqualityOperator
{
Equal = 1,
LessThan = 2,
GreaterThan = 4,
BeginsWith = 8,
EndsWith = 16,
Contains = 32,
All = Equal | LessThan | GreaterThan | BeginsWith
| EndsWith | Contains
}

.... I want a related textual description of "Greater Than", "Begins With",
etc. I don't want to use the text description of "BeginsWith" (that's one
word) by calling this line of code...

string format = Enum.Format(typeof(EqualityOperator),
EqualityOperator.BeginsWith, "g"); //Returns "BeginsWith" - one word

Because I want this "object" to be data bindable, I guess I could use the
CollectionBase object. I'm just looking for other ways/opinions on how to
do this.

Thanks,

Kyle
 
One way, would be to use the [Description()] attribute (found within
System.ComponentModel before each tag, so your enum would end as:

[Flags]
public enum EqualityOperator
{
[Description("Equal")]
Equal = 1,
[Description("Less Than")]
LessThan = 2,
[Description("Greater Than")]
GreaterThan = 4,
[Description("Begins With")]
BeginsWith = 8,
[Description("Ends With")]
EndsWith = 16,
[Description("Contains")]
Contains = 32,
[Description("All")]
All = Equal | LessThan | GreaterThan | BeginsWith
| EndsWith | Contains
}

And every time you want the description, use the following function:

public static string GetEnumDescription(object value)
{
string retVal = "";
try
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fieldInfo.GetCustomAttributes
(typeof(DescriptionAttribute), false);
retVal =
((attributes.Length>0)?attributes[0].Description:value.ToString());
}
catch( NullReferenceException )
{
//Occurs when we attempt to get description of an enum value
that does not exist
retVal = "Unknown";
}

return retVal;
}

Brendan
 
Hi,

I've done something like this in my own project and it works very well. The only
downside is the run-time performance cost of using the reflection API to get at
the description. We ended up caching the descriptions after the first access
through the reflection API.

Best regards,

Rodger

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>


Brendan said:
One way, would be to use the [Description()] attribute (found within
System.ComponentModel before each tag, so your enum would end as:

[Flags]
public enum EqualityOperator
{
[Description("Equal")]
Equal = 1,
[Description("Less Than")]
LessThan = 2,
[Description("Greater Than")]
GreaterThan = 4,
[Description("Begins With")]
BeginsWith = 8,
[Description("Ends With")]
EndsWith = 16,
[Description("Contains")]
Contains = 32,
[Description("All")]
All = Equal | LessThan | GreaterThan | BeginsWith
| EndsWith | Contains
}

And every time you want the description, use the following function:

public static string GetEnumDescription(object value)
{
string retVal = "";
try
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fieldInfo.GetCustomAttributes
(typeof(DescriptionAttribute), false);
retVal =
((attributes.Length>0)?attributes[0].Description:value.ToString());
}
catch( NullReferenceException )
{
//Occurs when we attempt to get description of an enum value
that does not exist
retVal = "Unknown";
}

return retVal;
}

Brendan


:

What I want to do is take the values of an enum and have a related textual
description for each item. For example, if I have an enum declared as the
following...

[Flags]
public enum EqualityOperator
{
Equal = 1,
LessThan = 2,
GreaterThan = 4,
BeginsWith = 8,
EndsWith = 16,
Contains = 32,
All = Equal | LessThan | GreaterThan | BeginsWith
| EndsWith | Contains
}

.... I want a related textual description of "Greater Than", "Begins With",
etc. I don't want to use the text description of "BeginsWith" (that's one
word) by calling this line of code...

string format = Enum.Format(typeof(EqualityOperator),
EqualityOperator.BeginsWith, "g"); //Returns "BeginsWith" - one word

Because I want this "object" to be data bindable, I guess I could use the
CollectionBase object. I'm just looking for other ways/opinions on how to
do this.

Thanks,

Kyle
 
Back
Top