Enum ToString with user friendly strings

后端 未结 23 1790
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 11:44

My enum consists of the following values:

private enum PublishStatusses{
    NotCompleted,
    Completed,
    Error
};

I want to be able to

23条回答
  •  既然无缘
    2020-11-22 12:32

    Some other more primitive options that avoid classes/reference types:

    • Array method
    • Nested struct method

    Array method

    private struct PublishStatusses
    {
        public static string[] Desc = {
            "Not Completed",
            "Completed",
            "Error"
        };
    
        public enum Id
        {
            NotCompleted = 0,
            Completed,
            Error
        };
    }
    

    Usage

    string desc = PublishStatusses.Desc[(int)PublishStatusses.Id.Completed];
    

    Nested struct method

    private struct PublishStatusses
    {
        public struct NotCompleted
        {
            public const int Id = 0;
            public const string Desc = "Not Completed";
        }
    
        public struct Completed
        {
            public const int Id = 1;
            public const string Desc = "Completed";
        }
    
        public struct Error
        {
            public const int Id = 2;
            public const string Desc = "Error";
        }            
    }
    

    Usage

    int id = PublishStatusses.NotCompleted.Id;
    string desc = PublishStatusses.NotCompleted.Desc;
    

    Update (03/09/2018)

    A hybrid of Extension Methods and the first technique above.

    I prefer enums to be defined where they "belong" (closest to their source of origin and not in some common, global namespace).

    namespace ViewModels
    {
        public class RecordVM
        {
            //public enum Enum { Minutes, Hours }
            public struct Enum
            {
                public enum Id { Minutes, Hours }
                public static string[] Name = { "Minute(s)", "Hour(s)" };
            }
        }
    }
    

    The extension method seems suited for a common area, and the "localized" definition of the enum now makes the extension method more verbose.

    namespace Common
    {
        public static class EnumExtensions
        {
            public static string Name(this RecordVM.Enum.Id id)
            {
                return RecordVM.Enum.Name[(int)id];
            }
        }   
    }
    

    A usage example of the enum and it's extension method.

    namespace Views
    {
        public class RecordView 
        {
            private RecordDataFieldList _fieldUnit;
    
            public RecordView()
            {
                _fieldUnit.List = new IdValueList
                {            
                    new ListItem((int)RecordVM.Enum.Id.Minutes, RecordVM.Enum.Id.Minutes.Name()),
                    new ListItem((int)RecordVM.Enum.Id.Hours, RecordVM.Enum.Id.Hours.Name())
                };
            }
    
            private void Update()
            {    
                RecordVM.Enum.Id eId = DetermineUnit();
    
                _fieldUnit.Input.Text = _fieldUnit.List.SetSelected((int)eId).Value;
            }
        }
    }
    

    Note: I actually decided to eliminate the Enum wrapper (and Name array), since it's best that the name strings come from a resource (ie config file or DB) instead of being hard-coded, and because I ended up putting the extension method in the ViewModels namespace (just in a different, "CommonVM.cs" file). Plus the whole .Id thing becomes distracting and cumbersome.

    namespace ViewModels
    {
        public class RecordVM
        {
            public enum Enum { Minutes, Hours }
            //public struct Enum
            //{
            //    public enum Id { Minutes, Hours }
            //    public static string[] Name = { "Minute(s)", "Hour(s)" };
            //}
        }
    }
    

    CommonVM.cs

    //namespace Common
    namespace ViewModels
    {
        public static class EnumExtensions
        {
            public static string Name(this RecordVM.Enum id)
            {
                //return RecordVM.Enum.Name[(int)id];
                switch (id)
                {
                    case RecordVM.Enum.Minutes: return "Minute(s)";                    
                    case RecordVM.Enum.Hours: return "Hour(s)";
                    default: return null;
                }
            }
        }   
    }
    

    A usage example of the enum and it's extension method.

    namespace Views
    {
        public class RecordView 
        {
            private RecordDataFieldList _fieldUnit
    
            public RecordView()
            {
                _fieldUnit.List = new IdValueList
                {            
                    new ListItem((int)RecordVM.Enum.Id.Minutes, RecordVM.Enum.Id.Minutes.Name()),
                    new ListItem((int)RecordVM.Enum.Id.Hours, RecordVM.Enum.Id.Hours.Name())
                };
            }
    
            private void Update()
            {    
                RecordVM.Enum eId = DetermineUnit();
    
                _fieldUnit.Input.Text = _fieldUnit.List.SetSelected((int)eId).Value;
            }
        }
    }
    

提交回复
热议问题