Need a way to map enum values to human friendly strings
-
We have various enumerated types in our system and we obviously want to present human readable values that can be easily configured by our UI folks rather than the cryptic identiofiers that we use in the code. Just wondering what facilities are available in .NET 3.0 to accomodate this kind of thing. In the old days, I would use a string resource and a mapping table for the lookup. Anybody got any cool/slick/modern ideas on this? Thanks NIK
-
We have various enumerated types in our system and we obviously want to present human readable values that can be easily configured by our UI folks rather than the cryptic identiofiers that we use in the code. Just wondering what facilities are available in .NET 3.0 to accomodate this kind of thing. In the old days, I would use a string resource and a mapping table for the lookup. Anybody got any cool/slick/modern ideas on this? Thanks NIK
The following will print "Blue" to the console:
namespace CSFoo { class Program { static void Main(string[] args) { Console.WriteLine(ETest.Blue); } } enum ETest { Red, White, Blue } }
-
The following will print "Blue" to the console:
namespace CSFoo { class Program { static void Main(string[] args) { Console.WriteLine(ETest.Blue); } } enum ETest { Red, White, Blue } }
I need to provide a secondary string description of the enum value. Rather than "Blue", I would want to map that value to a human friendly string such as "The Color Blue". Make sense?
-
I need to provide a secondary string description of the enum value. Rather than "Blue", I would want to map that value to a human friendly string such as "The Color Blue". Make sense?
-
I need to provide a secondary string description of the enum value. Rather than "Blue", I would want to map that value to a human friendly string such as "The Color Blue". Make sense?
namespace CSFoo { class Program { static void Main(string[] args) { Console.WriteLine("The Color {0}", ETest.Blue.ToString()); } } enum ETest { Red, White, Blue } }
-
namespace CSFoo { class Program { static void Main(string[] args) { Console.WriteLine("The Color {0}", ETest.Blue.ToString()); } } enum ETest { Red, White, Blue } }
should have extended subject to say "that can be easily modified by non-programmers outside of the .cs file" I think I can probably build a ResourceDictionary in XAML that would provide a basic lookup capability at application scope. Thus my UI guy can tweak the XAML and I can simply do a FindResource using the enum value identifier as the key.
-
should have extended subject to say "that can be easily modified by non-programmers outside of the .cs file" I think I can probably build a ResourceDictionary in XAML that would provide a basic lookup capability at application scope. Thus my UI guy can tweak the XAML and I can simply do a FindResource using the enum value identifier as the key.
Are you looking for something that can be modified at runtime or just modified by a "non-programmer" at design time? You can also look at using the DescriptionAttribute and using a little bit of reflection to retrieve the attribute value at runtime. I believe the strings used in the attribute constructor can be referenced from a resource file.
----------------------------- In just two days, tomorrow will be yesterday.
-
Are you looking for something that can be modified at runtime or just modified by a "non-programmer" at design time? You can also look at using the DescriptionAttribute and using a little bit of reflection to retrieve the attribute value at runtime. I believe the strings used in the attribute constructor can be referenced from a resource file.
----------------------------- In just two days, tomorrow will be yesterday.
No runtime modification or localization needs. Decided to go with a simple XAML declaration of strings in Application.Resources and use typename.value as key (ID) name. No hard coded string IDs, and easy for non-programmers to maintain (notepad) in a centralized location.