0

i have an enum like

public enum DecimailPrecision
        {
            One,
            Two,
        }

and class as

class DecimailPrecision1
        {
            public const string One = "#,##0.0";
            public const string Two = "#,##0.00";
        }

i want to retrieve const string from class with enum. i already doing this with if and switch as

 string format = string.Empty;
switch (value)
{
case DecimailPrecision.One:
     format = DecimailPrecision1.One.ToString(); break;
case DecimailPrecision.Two:
     format = DecimailPrecision1.Two.ToString(); break;
default:
     format = DecimailPrecision1.Two.ToString(); break;
}

if (value == "One"){
   format = DecimailPrecision1.One.ToString();}
else if (value == "Two"){
   format = DecimailPrecision1.Two.ToString();}
}

i need a better way because i have lot items in enum.

thanks.

chanti
  • 95
  • 1
  • 9

2 Answers2

5

Why not just create a Dictionary<DecimailPrecision, string> and hold the mappings in that?

That way you can simply look up your DecimailPrecision value in the dictionary and retrieve the appropriately mapped string.

You could even store the mapping in config and read it from that, so you wouldn't need to recompile your code to add new mappings.

To explicitly apply this to your code (I'd recommend changing the name of your consts to DecimalPrecisionFormat):

var precisionMap = new Dictionary<DecimailPrecision, string> 
{ 
    { DecimailPrecision.One, DecimailPrecision1.One }
  , { DecimailPrecision.Two, DecimailPrecision1.Two } 
};

var formatTwo = precisionMap[DecimailPrecision.Two];
Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
  • `+1` Great Answer Daniel you should provide OP with an example as well as how to call and or use the `Dictionary` – MethodMan Jun 06 '14 at 14:26
0

For similar needs we developed a custom attribute and a few extension methods.

Usage is like this.

public enum DecimailPrecision
{
    [EnumCode("#,##0.0")]
    One,

    [EnumCode("#,##0.00")]
    Two
}

string format = DecimailPrecision.One.GetCode();

It may be meaningless for your case but reverse is valid through like this

string format ="#,##0.00";

DecimailPrecision dp = format.ToEnum<DecimailPrecision>();

Extensions and Atrribute are like:

public static class EnumExtensions
{
    private static readonly Dictionary<Type, EnumCodePair[]> EnumCodeCache = new Dictionary<Type, EnumCodePair[]>();

    public static string GetCode(this Enum enumValue) {
        var codePairs = GetEnumCodePairs(enumValue.GetType());
        return codePairs.First(cp => Equals(cp.Enum, enumValue)).Code;
    }

    public static T ToEnum<T>(this string enumCode) {
        var codePairs = GetEnumCodePairs(typeof(T));
        return (T)codePairs.First(cp => Equals(cp.Code, enumCode)).Enum;
    }

    private static IEnumerable<EnumCodePair> GetEnumCodePairs(Type type) {
        if(!EnumCodeCache.ContainsKey(type)) {
            var enumFields = type.GetFields(BindingFlags.Public | BindingFlags.Static);

            var codePairs = new List<EnumCodePair>();

            foreach(var enumField in enumFields) {
                var enumValue = Enum.Parse(type, enumField.Name);

                var codePair = new EnumCodePair {
                    Enum = enumValue
                };

                var attrs = enumField.GetCustomAttributes(typeof(EnumCodeAttribute), false);

                codePair.Code = attrs.Length == 0
                                    ? enumField.Name
                                    : ((EnumCodeAttribute)attrs[0]).Code;

                codePairs.Add(codePair);
            }

            EnumCodeCache.Add(type, codePairs.ToArray());
        }
        return EnumCodeCache[type];
    }

    class EnumCodePair
    {
        public object Enum { get; set; }
        public string Code { get; set; }
    }
}

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class EnumCodeAttribute : Attribute
{
    public EnumCodeAttribute(string code) {
        Code = code;
    }

    public string Code { get; private set; }
}
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78