Creating user friendly strings for enum values

In this post, I'll show you how to create a custom attribute for enum values to assign extra information to an enum. In this case, we'll be associating a user friendly display name.

Order Status Screenshot

In .NET, enums are basically a list of constant values. It's a way to assign code friendly names to a group of values. For example, an order status:

enum OrderStatus
    NewOrder = 1,
    Processing = 2,
    Shipped = 3

In the above example, the numeric values are optional, they will be assigned automatically if you don't supply them.

What if you want to display the enum values in a drop down list, so that an admin can change the status of an order? One option is to loop through the enum, and display the code value ("NewOrder, "Processing", etc.). Another option is to have a "switch" statement or a "for" statement that allows you to hard code a user friendly value.

Another option is to create a custom attribute to add additional meta data:

public class EnumValueDataAttribute : Attribute
    private string _name;
    public string Name
        get{ return _name; }
        set{ _name = value; }

Now we can use the attribute to the enum I showed you previously:

enum OrderStatus
    [EnumValueData(Name="New Order")]
    NewOrder = 1,
    [EnumValueData(Name="In Process")]
    Processing = 2,
    Shipped = 3

Now we can create a method that gets the data in a useful format:

public static Dictionary<int, string> GetListItems(Type enumType)
    if (!enumType.IsEnum)
        throw new ApplicationException("GetListItems does not support non-enum types");
    Dictionary<int, string> list = new Dictionary<int, string>();
    foreach(FieldInfo field in enumType.GetFields(BindingFlags.Static ' BindingFlags.GetField ' BindingFlags.Public))
        int value;
        string display;
        value = (int)field.GetValue(null);
        display = Enum.GetName(enumType, value);
        foreach(Attribute currAttr in field.GetCustomAttributes(true))
            EnumValueDataAttribute valueAttribute = currAttr as EnumValueDataAttribute;
            if (valueAttribute != null)
                display = valueAttribute.Name;
        list.Add(value, display);
    return list;

Notice that the method will also work even if the "EnumValueData" attribute is missing, so you don't have to supply it for every value.

Based off of this code, you could easily create additional fields to associate with enum values. Basically what we're doing is creating a simple database table in our code. You'll have to decide if this is a good idea for your project.

Like this post? Please share it!

See a mistake? Edit this post!

Subtext blog platform feature requests

For this blog, I'm currently using WordPress. For my personal blog, I chose to use SubText. SubText is an ASP.NET based installable blogging platform that was originally based on the .Text source code. I initially tried to stay with a .NET based solution, so that I would be able to fix it if something went wrong.

It's a great platform, and I have few complaints. It's simple, and it does its job extremely well. I do however, have some suggestions (if they're not already in there).


Output Caching - For low traffic sites, this doesn't really make a difference. What I am concerned with is the chance that my site could end up on the front page of Slashdot or Digg. The only hope of survival is output caching. Even caching for 30 seconds could make a huge difference.

As an extra bonus, a SQL dependency cache could be used so that changes still show up immediately.

Meta Description - The keywords are not very important, but it would be nice to have a nice meta description. Google usually DOES use the meta description tag if you supply one.The all-in-one seo pack for Wordpress works pretty slick, because it gives you a place to change all of your SEO settings.

301 Redirects for non-standard URL's - Many of the search spiders are not very intelligent. It would be nice if the URL's were forced to be a certain pattern. For example, if someone links to instead of, it doesn't get redirected. That means that the search engines will potentially index both. The same goes for the capitalization of the post names. I would like it to redirect to the all lower case version of the URL's.

NoFollow, NoIndex - It would be nice if a lot of the architecture pages (categories, archives, tags), were marked as NoIndex. It would also be nice if links to those pages were marked as NoFollow for safety. There's no reason to pass on your PageRank to pages without any unique content. There is also no reason for them to show up in the search engines.

HTML Cleanup - This feature is much less important to me. It would be neat to use HTML Tidy to clean up the HTML if it's malformed. I used to write my post HTML by hand until Windows Live Writer matured.

I do, however, have lots of old posts that have potentially malformed HTML. My plan is to eventually write a utility that will clean them up.

I've been seriously contemplating converting it to Wordpress. It's nice being on one of the most popular platform. The fact that I can use add-ons to make it exactly what I want is certainly a plus.

Let's see what you can do Subtext!

Like this post? Please share it!

See a mistake? Edit this post!

Why I'm a better software developer than you

What makes one developer better than another? Shouldn't we all be performing at the same level? Of course not, we're not sewing buttons on an assembly line. We're using every bit of our intelligence to create something that we can only begin to understand.

Software Geek

  • I think logically. Computers don't care how you feel, and your opinion doesn't matter. All that matters is if you write your code exactly the way the computer dictates.
  • I constantly look for better ways of doing things. I subscribe to a good number of development blogs. I alone cannot always come up with the best way to solve a problem, but somebody somewhere probably can.
  • I read books. Joel says that most programmers have stopped reading books. What a shame. Blogs are great for snippets, but it's rare that they cover a topic well from start to finish. Blogs are the ADD version of books.
  • I don't stop thinking about problems and how to solve them through automation. Sometimes I'll wake up in the middle of the night, and I can't get back to sleep until I write some code that I can't get out of my head.
  • I have side projects that I think are interesting, and give me a chance to try things that I might not want to try on my production code at work. Yes, my side projects distract me at work, but the knowledge I gain pays back the time I lost.
  • I have a tech blog. I suggest all developers start a blog and give back to the community. If you solve a problem, we want to hear about it! At the very least, it will give you an opportunity to formalize your ideas, which will either reinforce them, or make you realize you were wrong. You might also get some great feedback.
  • I try to prove myself wrong (aka objective). Everyone wants to be right. I try to prove myself wrong when appropriate. One of the hardest things in the world for a developer to do is say that the code they just spent a week writing is useless. Maybe it is, don't fight it, work with it.
  • I keep up with the latest technologies, and force myself to try them.
  • I have a relatively good understanding of how the computer hardware and software works. I've met too many developers that barely know how to turn on a computer.
  • I'm great at writing Google queries.
  • I'm not just in it for the money. I actually enjoy what I do. I had a job interview where the guy that would have been my boss told me a story about how he was brought in off the street and thrown into managing their software projects. When the software industry starts getting rough, who do you think is the first person to go?
  • I'm sympathetic to the users pain. If I can share their pain, I'll want to fix it and prevent it.
  • I realize my code will never be perfect, so I try to make it testable and modular. I set up processes that try to minimize the effect of my human error.
  • I don't think Microsoft is evil, and I don't think they're a saint. They're a big company. Some of the stuff they write is crap, some is amazing. The same is true for any other company out there.
  • I learn from my mistakes. I try to put at least 2 checks in place to avoid any past mistakes. If one check fails, I'll have the other.
  • When I'm asked to solve a problem, I think above the problem, and determine if it's a problem that even needs solved.

In case it isn't clear, the theme of this post was written as a joke, although the concepts are useful

Like this post? Please share it!

See a mistake? Edit this post!

Jason Young I'm Jason Young, software engineer. This blog contains my opinions, of which my employer - Microsoft - may not share.

@ytechieGitHubLinkedInStack OverflowPersonal VLOG