Click here to Skip to main content
Licence CPOL
First Posted 7 Sep 2007
Views 20,980
Downloads 94
Bookmarked 20 times

Making an Enum Readable (The Lazy Way)

By | 12 Sep 2007 | Article
Making an Enum Readable without any hassle...
Screenshot - enum.jpg

Introduction

Many people including Grant Frisken in this article have shown of ways to localise and make an enum readable. These are great articles. However there are many times when you are in a hurry and have not got the time to "readabalize" the text but still want to display the enum values in the code. Another point is that many enums that we use are not owned by us, and therefore we cannot use his methods.

I recently made a whole framework for my company and I used enums as the keys for all lookups, etc. I also used enums for error codes like NoReply... The advantage of enums is that you can be sure that the text only occurs once and at the same time you only need to enter the text once.

(You could do it like this)...

const string NoReply = "No Reply"

... but that means entering the same text twice.

Also if you change your mind and decide that "Long Wait For Reply" would be better, you risk having the customer see something different than you...

Using the Code

To use convert the enum values to text, use the following:

enum Example
{
RedAndBlue,
BlueAndGreen,
AnotherColour,
Whatever,
SomethingElse,
UseYourImagination,
}

//Convert to string
string redAndBlue = EnumConverter.AutoSpace(Example.RedAndBlue);
//And back
Example example = (Example) 
    Enum.Parse(typeof(Example), EnumConverter.RemoveSpaces(redAndBlue));

You can convert the whole enum like this:

string[] example = EnumConverter.AutoSpace(typeof(Example));

That's it. I know it's not a huge step for mankind, but it can help to make a big difference quickly. Especially if you are under time pressure...

The disadvantage with this solution is that you cannot have words starting with small letters. However, you still can use the ideas of Grant Frisken to make things better once it is working.

This example also works for strings so you can even make class names readable...

The main part of this code is here: The text is split by searching for capital letters or numbers...

static public string AutoSpace(string sInput)
{
    StringBuilder sOut = new StringBuilder();
    for (int i = 0; i < sInput.Length; i++)
        {
        char c = sInput[i];
        if (c >= 'A' && c <= 'Z')
        {
        if (i > 0 && (sInput[i - 1] >= 'A' 
        && sInput[i - 1] <= 'Z'))
            sOut.Append(c);
        else if (i == 0)
            sOut.Append(c);
        else
            sOut.Append(" " + c);

        }
        else if (c == '_')
            sOut.Append('_');
        else if (c >= '0' && c <= '9')
            {
            if (i > 0 && (sInput[i - 1] >= 'a' 
            && sInput[i - 1] <= 'z'))
                sOut.Append(" " + c);
            else
                sOut.Append(c);
            }
        else
            sOut.Append(c);
        }
    return sOut.ToString();
}

A possible modification could include a list of small words (like if, or, etc.) that would always be written in small letters.

Please let me know if this was useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Joe Sonderegger

Software Developer
RUAG Aerospace
Switzerland Switzerland

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberJison Lorentz22:23 22 Jul '09  
GeneralMy vote of 1 PinmemberMario Costarica0:52 22 Jul '09  
GeneralSuggestion PinmemberNico W10:01 29 Oct '07  
GeneralRe: Suggestion PinmemberJoe Sonderegger22:26 29 Oct '07  
GeneralRe: Suggestion PinmemberJoe Sonderegger6:12 22 Nov '07  
GeneralGrant Frisken idea is not so good either... PinmemberSAKryukov8:45 18 Sep '07  
GeneralI was not right. My apologies. PinmemberSAKryukov5:49 19 Sep '07  
General(Too) Much Ado About Nothing; look at the real thing PinmemberSAKryukov8:30 18 Sep '07  
GeneralRe: (Too) Much Ado About Nothing; look at the real thing PinmemberJoe Sonderegger20:21 18 Sep '07  
GeneralRe: (Too) Much Ado About Nothing; look at the real thing PinmemberSAKryukov6:02 19 Sep '07  
GeneralRe: (Too) Much Ado About Nothing; look at the real thing PinmemberSonOfPirate6:11 19 Sep '07  
GeneralUnderstood (Re: (Too) Much Ado About Nothing; look at the real thing) PinmemberSAKryukov10:42 19 Sep '07  
Generalquick but dirty PinmemberHerre Kuijpers1:48 12 Sep '07  
GeneralImage not shown PinmemberRudolf Jan Heijink4:24 9 Sep '07  
GeneralRe: Image not shown PinmemberJoe Sonderegger22:09 9 Sep '07  
GeneralMissing Parenthesis PinmemberBarryDorman9:17 7 Sep '07  
GeneralRe: Missing Parenthesis PinmemberJoe Sonderegger22:10 9 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 13 Sep 2007
Article Copyright 2007 by Joe Sonderegger
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid