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. C#
  4. no white spaces in enum

no white spaces in enum

Scheduled Pinned Locked Moved C#
question
8 Posts 6 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.
  • M Offline
    M Offline
    MVPenn
    wrote on last edited by
    #1

    I have created a custom web control and I've added a property that will display a list of given values. I created a enum type and set my enumerators list values, but I can't have any white spaces in my list. How can I show list values that have white spaces using the enum type? Any suggestions would be appreciated! Thanks in advance. MVPenn

    D E 2 Replies Last reply
    0
    • M MVPenn

      I have created a custom web control and I've added a property that will display a list of given values. I created a enum type and set my enumerators list values, but I can't have any white spaces in my list. How can I show list values that have white spaces using the enum type? Any suggestions would be appreciated! Thanks in advance. MVPenn

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You can't. Enums are for your code to understand. Essentially like giving a code-usable name to a value, kind of like a variable. They're not meant for use as a UI element. What are trying to do with this?

      Dave Kreskowiak Microsoft MVP - Visual Basic

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You can't. Enums are for your code to understand. Essentially like giving a code-usable name to a value, kind of like a variable. They're not meant for use as a UI element. What are trying to do with this?

        Dave Kreskowiak Microsoft MVP - Visual Basic

        M Offline
        M Offline
        MVPenn
        wrote on last edited by
        #3

        I wanted to show a more "user friendly" description in the listbox on my web control property. For example: public enum Diseases { Hepatitis A, Hepatitis B, ...} public enum fonts {Arial Black, Arial Narrow, Arial, ...} I'm not sure if I should be using the enum type to populate a list property on a custom web control. Is there a better solution to accomplish this? I'm relatively new to development and would appreciate any suggestions that you might have on this. Thanks for your response. MVPenn

        S P W D 4 Replies Last reply
        0
        • M MVPenn

          I wanted to show a more "user friendly" description in the listbox on my web control property. For example: public enum Diseases { Hepatitis A, Hepatitis B, ...} public enum fonts {Arial Black, Arial Narrow, Arial, ...} I'm not sure if I should be using the enum type to populate a list property on a custom web control. Is there a better solution to accomplish this? I'm relatively new to development and would appreciate any suggestions that you might have on this. Thanks for your response. MVPenn

          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          MVPenn wrote:

          I'm not sure if I should be using the enum type to populate a list property on a custom web control.

          Obviously you can't, at least not the way you want to.

          MVPenn wrote:

          Is there a better solution to accomplish this?

          The easiest thing that comes to my mind is declaring a simple string array:

          public string[] Diseases = new string[] { "Hepatitis A", "Hepatitis B" };


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          1 Reply Last reply
          0
          • M MVPenn

            I wanted to show a more "user friendly" description in the listbox on my web control property. For example: public enum Diseases { Hepatitis A, Hepatitis B, ...} public enum fonts {Arial Black, Arial Narrow, Arial, ...} I'm not sure if I should be using the enum type to populate a list property on a custom web control. Is there a better solution to accomplish this? I'm relatively new to development and would appreciate any suggestions that you might have on this. Thanks for your response. MVPenn

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Try this:

            public static string EnumHelper(Enum value)
            {
              // Set the description to the name of the enumeration.
              // That way, if there's no Description attribute, we still
              // have a description to return.
              description = value.ToString();
            
              MemberInfo[] mi = value.GetType().GetMember(value.ToString());
              if (mi != null && mi.Length > 0)
              {
                object[] att = mi[0].GetCustomAttributes(typeof(Description), false);
                if (att != null  && att.Length > 0)
                  description = ((Description)att[0]).Text;
              }
              return description;
            }
            

            Then, in your enumeration all you need to do is:

            public enum Diseases
            {
              [Description("Hepatitis A")]
              HepatitisA,
              Eczema,
            }
            

            Then, in your code you will do the following:

            string hepA = EnumHelper(Diseases.HepatitisA);
            

            This returns Hepatitis A.

            string ex = EnumHelper(Diseases.Eczema);
            

            returns Eczema. I hope this helps.

            the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
            Deja View - the feeling that you've seen this post before.

            1 Reply Last reply
            0
            • M MVPenn

              I wanted to show a more "user friendly" description in the listbox on my web control property. For example: public enum Diseases { Hepatitis A, Hepatitis B, ...} public enum fonts {Arial Black, Arial Narrow, Arial, ...} I'm not sure if I should be using the enum type to populate a list property on a custom web control. Is there a better solution to accomplish this? I'm relatively new to development and would appreciate any suggestions that you might have on this. Thanks for your response. MVPenn

              W Offline
              W Offline
              Wjousts
              wrote on last edited by
              #6

              MVPenn wrote:

              public enum Diseases { Hepatitis A, Hepatitis B, ...} public enum fonts {Arial Black, Arial Narrow, Arial, ...}

              In the past I've done something like this: public enum Diseases { Hepatitis_A, Hepatitis_B, ...} And when you need to display the "friendly" values just use the String.Replace function to get rid of the underscores.

              1 Reply Last reply
              0
              • M MVPenn

                I wanted to show a more "user friendly" description in the listbox on my web control property. For example: public enum Diseases { Hepatitis A, Hepatitis B, ...} public enum fonts {Arial Black, Arial Narrow, Arial, ...} I'm not sure if I should be using the enum type to populate a list property on a custom web control. Is there a better solution to accomplish this? I'm relatively new to development and would appreciate any suggestions that you might have on this. Thanks for your response. MVPenn

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                I've never hard coded anything in a enum like this. I've always put my options like this is a database or XML file. It's easier to modify that file than it is to modify the code and deploy a new app if the options change.

                Dave Kreskowiak Microsoft MVP - Visual Basic

                1 Reply Last reply
                0
                • M MVPenn

                  I have created a custom web control and I've added a property that will display a list of given values. I created a enum type and set my enumerators list values, but I can't have any white spaces in my list. How can I show list values that have white spaces using the enum type? Any suggestions would be appreciated! Thanks in advance. MVPenn

                  E Offline
                  E Offline
                  e laj
                  wrote on last edited by
                  #8

                  Hi MVPenn, Create your own attribute (you may wish to call it EnumDescription). You can find some examples over the net. For example: Example Recall that if you want to display friendly names for property name - you can use the "DisplayName" attribute. Unfortunately, this attribute can not be applied for enum members. Hence (as i said before) have to create your own enum description attribute. elaj

                  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