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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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,
|
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
|
by: paii, Ron |
last post by:
Is there a setting to not word wrap text on a unbound text box. in A97?
| |
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',
...
|
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}
}
|
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...
|
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.
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |