Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Making an Enum Readable (The Lazy Way)

Rate me:
Please Sign up or sign in to vote.
4.17/5 (19 votes)
12 Sep 2007CPOL2 min read 43.9K   141   20   17
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)...

C#
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:

C#
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:

C#
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...

C#
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)


Written By
Software Developer RUAG Aerospace
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Jison Lorentz22-Jul-09 22:23
Jison Lorentz22-Jul-09 22:23 
GeneralMy vote of 1 Pin
Mario Costarica22-Jul-09 0:52
Mario Costarica22-Jul-09 0:52 
GeneralSuggestion Pin
Nico W29-Oct-07 10:01
Nico W29-Oct-07 10:01 
GeneralRe: Suggestion Pin
Joe Sonderegger29-Oct-07 22:26
Joe Sonderegger29-Oct-07 22:26 
GeneralRe: Suggestion Pin
Joe Sonderegger22-Nov-07 6:12
Joe Sonderegger22-Nov-07 6:12 
GeneralGrant Frisken idea is not so good either... Pin
Sergey Alexandrovich Kryukov18-Sep-07 8:45
mvaSergey Alexandrovich Kryukov18-Sep-07 8:45 
GeneralI was not right. My apologies. Pin
Sergey Alexandrovich Kryukov19-Sep-07 5:49
mvaSergey Alexandrovich Kryukov19-Sep-07 5:49 
General(Too) Much Ado About Nothing; look at the real thing Pin
Sergey Alexandrovich Kryukov18-Sep-07 8:30
mvaSergey Alexandrovich Kryukov18-Sep-07 8:30 
GeneralRe: (Too) Much Ado About Nothing; look at the real thing Pin
Joe Sonderegger18-Sep-07 20:21
Joe Sonderegger18-Sep-07 20:21 
GeneralRe: (Too) Much Ado About Nothing; look at the real thing Pin
Sergey Alexandrovich Kryukov19-Sep-07 6:02
mvaSergey Alexandrovich Kryukov19-Sep-07 6:02 
Pardon me, Joe.
In my approach, I NEVER enter the text twice. Please look again at my sample.
I only enter the attribute when there is a difference between enum name obtained via reflection and the desired name. I still think your approach does not pay off.

In my real-life programming practice I could see that just adding a space character almost never cover all the cases: sometimes it needs dash or something else. Another case is the use of abbreviation: "WindowsApi" must give "Windows API", not "Windows Api"; and, alternatively, "WindowsAPI" (not recommended by FxCop) must not give "Windows A P I". You will rarely cover all possible cases, which may pop up while your application code base grows. It just does not worth the effort, when a natural solution is so easy and, well... natural: the source of grammar knowledge is the one who enters the text in the project.

I hope we can close this discussion here.

Sergey A Kryukov

http://www.SAKryukov.org
GeneralRe: (Too) Much Ado About Nothing; look at the real thing Pin
SonOfPirate19-Sep-07 6:11
SonOfPirate19-Sep-07 6:11 
GeneralUnderstood (Re: (Too) Much Ado About Nothing; look at the real thing) Pin
Sergey Alexandrovich Kryukov19-Sep-07 10:42
mvaSergey Alexandrovich Kryukov19-Sep-07 10:42 
Generalquick but dirty Pin
Herre Kuijpers12-Sep-07 1:48
Herre Kuijpers12-Sep-07 1:48 
GeneralImage not shown Pin
Rudolf Jan9-Sep-07 4:24
Rudolf Jan9-Sep-07 4:24 
GeneralRe: Image not shown Pin
Joe Sonderegger9-Sep-07 22:09
Joe Sonderegger9-Sep-07 22:09 
GeneralMissing Parenthesis Pin
Barry Dorman7-Sep-07 9:17
Barry Dorman7-Sep-07 9:17 
GeneralRe: Missing Parenthesis Pin
Joe Sonderegger9-Sep-07 22:10
Joe Sonderegger9-Sep-07 22:10 

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

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