jeudi 20 octobre 2011

Converting Enum values to user frendly strings

    public enum ErrorLevel
    public static class ErrorLevelExtensions
        public static string ToLocalizedString(this ErrorLevel me)
            switch (me)
                case ErrorLevel.None:
                    return "Everything is OK";
                case ErrorLevel.Low:
                    return "SNAFU, if you know what I mean.";
                case ErrorLevel.High:
                    return "Reaching TARFU levels";
            return "";
Then it is easy to use :
   1:  Console.WriteLine(ErrorLevel.Low.ToLocalizedStrings())

This uses Extensions Methods Share/Bookmark

Aucun commentaire: