473,888 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 EqualityOperato r
{
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(typ eof(EqualityOpe rator),
EqualityOperato r.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 2503
One way, would be to use the [Description()] attribute (found within
System.Componen tModel before each tag, so your enum would end as:

[Flags]
public enum EqualityOperato r
{
[Description("Eq ual")]
Equal = 1,
[Description("Le ss Than")]
LessThan = 2,
[Description("Gr eater Than")]
GreaterThan = 4,
[Description("Be gins With")]
BeginsWith = 8,
[Description("En ds With")]
EndsWith = 16,
[Description("Co ntains")]
Contains = 32,
[Description("Al l")]
All = Equal | LessThan | GreaterThan | BeginsWith
| EndsWith | Contains
}

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

public static string GetEnumDescript ion(object value)
{
string retVal = "";
try
{
FieldInfo fieldInfo = value.GetType() .GetField(value .ToString());
DescriptionAttr ibute[] attributes =
(DescriptionAtt ribute[])fieldInfo.GetC ustomAttributes
(typeof(Descrip tionAttribute), false);
retVal =
((attributes.Le ngth>0)?attribu tes[0].Description:va lue.ToString()) ;
}
catch( NullReferenceEx ception )
{
//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 EqualityOperato r
{
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(typ eof(EqualityOpe rator),
EqualityOperato r.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.SequenceDia gramEditor.com>
Brendan Grant wrote:
One way, would be to use the [Description()] attribute (found within
System.Componen tModel before each tag, so your enum would end as:

[Flags]
public enum EqualityOperato r
{
[Description("Eq ual")]
Equal = 1,
[Description("Le ss Than")]
LessThan = 2,
[Description("Gr eater Than")]
GreaterThan = 4,
[Description("Be gins With")]
BeginsWith = 8,
[Description("En ds With")]
EndsWith = 16,
[Description("Co ntains")]
Contains = 32,
[Description("Al l")]
All = Equal | LessThan | GreaterThan | BeginsWith
| EndsWith | Contains
}

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

public static string GetEnumDescript ion(object value)
{
string retVal = "";
try
{
FieldInfo fieldInfo = value.GetType() .GetField(value .ToString());
DescriptionAttr ibute[] attributes =
(DescriptionAtt ribute[])fieldInfo.GetC ustomAttributes
(typeof(Descrip tionAttribute), false);
retVal =
((attributes.Le ngth>0)?attribu tes[0].Description:va lue.ToString()) ;
}
catch( NullReferenceEx ception )
{
//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 EqualityOperato r
{
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(typ eof(EqualityOpe rator),
EqualityOperato r.BeginsWith, "g"); //Returns "BeginsWith " - one word

Because I want this "object" to be data bindable, I guess I could use the
CollectionBas e 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
9564
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 change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
2
2085
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 seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the Enums 'Car' and 'House Public Class Fo Public Enum Ca For Chevrole Toyot
7
4729
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
1663
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}; EditTypeA LineType; // LineType holds widget type, and will be either Currency, Percent, LabeledNumber or Text
5
9887
by: paii, Ron | last post by:
Is there a setting to not word wrap text on a unbound text box. in A97?
2
13037
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 for the cart. This ultimatly takes 3 different flat file databases and merges them all into one. The current problem is with the following command: update .dbo.products set tp.thumbnail = 'graphics/00000001/thumbs/'+td.ProdImage+'.jpg', ...
3
1562
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
1355
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 and robustability point of view to choose from a list of predefined choices when calling functions. Well, of course enums are good for this, so I could have something like this. In which a webfrom textboxm gets a css class attribute which equals...
8
8796
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. BRgds, Daniel.
0
10778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4244
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.