Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Need a way to map enum values to human friendly strings

Need a way to map enum values to human friendly strings

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpdesignquestionlearning
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicknotyet
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • N nicknotyet

      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

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      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 } }

      N 1 Reply Last reply
      0
      • G George L Jackson

        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 } }

        N Offline
        N Offline
        nicknotyet
        wrote on last edited by
        #3

        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?

        D G 2 Replies Last reply
        0
        • N nicknotyet

          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?

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          The easiest way to do that would be to use a hashtable to map "Blue" to "The Color Blue".

          -- Rules of thumb should not be taken for the whole hand.

          1 Reply Last reply
          0
          • N nicknotyet

            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?

            G Offline
            G Offline
            George L Jackson
            wrote on last edited by
            #5

            namespace CSFoo { class Program { static void Main(string[] args) { Console.WriteLine("The Color {0}", ETest.Blue.ToString()); } }   enum ETest { Red, White, Blue } }

            N 1 Reply Last reply
            0
            • G George L Jackson

              namespace CSFoo { class Program { static void Main(string[] args) { Console.WriteLine("The Color {0}", ETest.Blue.ToString()); } }   enum ETest { Red, White, Blue } }

              N Offline
              N Offline
              nicknotyet
              wrote on last edited by
              #6

              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.

              S 1 Reply Last reply
              0
              • N nicknotyet

                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.

                S Offline
                S Offline
                Scott Dorman
                wrote on last edited by
                #7

                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.

                N 1 Reply Last reply
                0
                • S Scott Dorman

                  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.

                  N Offline
                  N Offline
                  nicknotyet
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups