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