Is there any alternative way of writing this switch statement(C#3.0)

Posted by Newbie on Stack Overflow See other posts from Stack Overflow or by Newbie
Published on 2010-05-08T08:02:38Z Indexed on 2010/05/08 8:08 UTC
Read the original article Hit count: 140

Filed under:

Can it be done in a better way

public static EnumFactorType GetFactorEnum(string str)
        {
            Standardization e = new Standardization();
            switch (str.ToLower())
            {
                case "beta":
                    e.FactorType = EnumFactorType.BETA;
                    break;
                case "bkp":
                    e.FactorType = EnumFactorType.BOOK_TO_PRICE;
                    break;
                case "yld":
                    e.FactorType = EnumFactorType.DIVIDEND_YIELD;
                    break;
                case "growth":
                    e.FactorType = EnumFactorType.GROWTH;
                    break;
                case "mean":
                    e.FactorType = EnumFactorType.MARKET_CAP;
                    break;
                case "momentum":
                    e.FactorType = EnumFactorType.MOMENTUM;
                    break;
                case "size":
                    e.FactorType = EnumFactorType.SIZE;
                    break;
                case "stat_fact1":
                    e.FactorType = EnumFactorType.STAT_FACT_1;
                    break;
                case "stat_fact2":
                    e.FactorType = EnumFactorType.STAT_FACT_2;
                    break;
                case "value":
                    e.FactorType = EnumFactorType.VALUE;
                    break;
            }
            return e.FactorType;
        }

If I create a Static class(say Constatant) and declare variable like

public static string BETA= "beta";

and then if I try to put that in the Case expression like

Case Constants.BETA : e.FactorType = EnumFactorType.BETA;
                        break;

then the compiler will report error.(quite expected)

So is there any other way?(I canot change the switch statement)

Using C#3.0

Thanks

© Stack Overflow or respective owner

Related posts about c#3.0