Click here to Skip to main content
13,193,590 members (50,011 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello all

I need to associate enumeration to the ‘real world values’ _and vice versa_.

At the moment the only idea I have is, to make my own (generic) class which uses .net ‘Dictionary’ e.g. key= enum, value= ‘real world value’ and then build the reverse mapping -using again 'Dictionary’– internally.

Enum          Real world Values’
-------	      ------------------------
enum1	      "x"
enum2         "thesecond"
.	      .	
.             .
enumN        "whatever"

N.B: In my case the ‘Real world Values’ are usually single char values, but I want to make it a little bit more general in the sense of ‘Real world Values’ can be any object witch supports any kind of unique ‘key’ (based on one or multiply properties.

Is there a better way?

To be honest: My English is to weak to find the right keywords for google.

Thanks in advance. Regards.
Posted 3-Jul-12 6:40am
Updated 3-Jul-12 6:52am
By the way, this is an interesting and practically important question, for UI and other applications, so I voted 5.
Fortunately, I have a work addressing exactly this problem, please see my answer.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I have a comprehensive solution explained in my CodeProject article:
Human-readable Enumeration Meta-data[^].

The solution is based on .NET attributes. You can have separate attributes for each enumeration member each giving it a human readable name, which is trivial, because this solution is not globalized, so it cannot be localized. A more advanced solution is using the attributes redirecting to the resources embedded in the assembly, something which does not use any sting constants in the code (which is very important for support) and globalized in first place, that is, ready for localization. I provided both approaches in my article.

See also other works referenced in the section "Other Approaches" and reference to my previous article on enumeration problem.


I don't think your English is problem here. I also have done some search before publishing the article and did find some solutions which did not satisfy me, that's why I developed mine.

idle63 3-Jul-12 14:38pm
Thank you very much for your prompt response. It will take time to go through and understand. It seems your class does support much more then I need.

I don't know whether I expressed my ‘key point’ clear enough. I’m mainly interested having an efficient (mainly comfortable) conversion enum<->realworld. e.g. I receive a value from a remote device and have to convert it to the applications enum. Another time my application gives me the enum and I have to send a device specific value to the remote device (sorry I can not explain it better).
Regards, Bruno
In my understanding, "real world" value, if it is a string as shown in your table in your question is as "real" as it is "human-readable", not only in the sense of human-readable typography (which often goes beyond the valid language ID format), but also carries some real-life semantic meaning.

However, even if I did no understand you fully and you mean something else, you can have a broader look at the problems: some mapping (more exactly, one-to-one correspondence) between enumeration members and some semantically significant values, and... use exact same approach I demonstrated in my article. Mapping with human-readable "names" corresponding to the enumeration members is just one application of this technique, but it can be anything else.

The .NET attribute is the most natural method of expressing one-to-one correspondence between members and any other object directly in the source code, because of its "in-place" syntax. Which is good for maintenance of the code.

So, think about it and, if you agree it's reasonable, please accept the answer formally (green button).
If this is still not clear or you have other concerns, we can discuss it. The problem is interesting enough...

--SA particular, the enumeration is the right method to for mapping something to some hardware-specific value you are interested in.

My approach still works, but you also should not forget about more natural approach in case the values are integers (for example, integer values representing hardware status or something). This is defining underlying integer values for each enumeration member, for example:

enum DeviceStatus {
Ready = 0,
Error = 0xFF,
Waiting = 0x1A,
IntegerResult = 0x2,
FloatingResult = 0x4,

Moreover, you can use integer values not prescribed in the enumeration type definition, for "general case" result. That is, you can assign any arbitrary value to a variable of the type DeviceStatus. Besides, you can combine consecutive values with bit-mapped values in the same type (devote some bits to one part, other bits to other).

Working with underlying integer values is a delicate thing I tried to describe in detail in my previous CodeProject work "Enumeration Types do not Enumerate! Working around .NET and Language Limitations":

Pleas see.
idle63 3-Jul-12 15:44pm
Yes it works for me. Thank you very much for your time.
Regards, Bruno
You are very welcome.
Good luck, call again.
Espen Harlinn 3-Jul-12 16:17pm
Excellent reply :-D
Thank you, Espen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171018.2 | Last Updated 3 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100