473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Enums and Text Descriptions

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

Nov 17 '05 #1
2 2479
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
"Kyle Novak" wrote:
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

Nov 17 '05 #2
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 Grant wrote:
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
"Kyle Novak" wrote:

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

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
7
by: Wolf | last post by:
Hi all Who can Help me with this one? if I have this Enums: #region Title public enum Title: short { NA = 0,
3
by: lou zion | last post by:
hi all, i'm trying to use a classes enums in another class, but can't seem to find the right syntax. i've got class A which has: enum EditTypeA {Currency, Percent, LabeledNumber, Text};...
5
by: paii, Ron | last post by:
Is there a setting to not word wrap text on a unbound text box. in A97?
2
by: Azzuron | last post by:
We are trying to translate some raw flat data into a cart. What we have done is BCP the raw files into the database, and then we begin to run a series of SQL commands to copy the data and format it...
3
by: =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post by:
I have a class as follows: class GamesConsole { public int iReference; public enum Maker {Nintendo, Sega, Sony, Panasonic} }
4
by: Just Me | last post by:
I keep hitting the same minor problem, but dont have a really great solution. So Im looking for ideas. There are many instances where I want to force the developer ( me ) from a readability...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.