Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 771421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:39:03+00:00 2026-05-14T18:39:03+00:00

Can it be done in a better way public static EnumFactorType GetFactorEnum(string str) {

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:39:04+00:00Added an answer on May 14, 2026 at 6:39 pm

    You could define a Dictionary<string, EnumFactorType> as a “mapping” and use that instead. It’s essentially the same logic but can be a bit more readable.

    First, the mapping object:

    private Dictionary<string, EnumFactorType> _mapping = new Dictionary<string, EnumFactorType>
    {
        { "beta", EnumFactorType.BETA },
        { "bkp", EnumFactorType.BOOK_TO_PRICE },
        // etc
    }
    

    Then in your method:

    e.FactorType = _mapping[str.ToLower()];
    

    You may want to call _mapping.ContainsKey(str) first just to be sure the mapping exists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can it be done or the only way is to configure it on IIS?
What can be done to resolve the exception: CrystalDecisions.CrystalReports.Engine.InternalException: Failed to open a rowset.
I think it can be done by applying the transformation matrix of the scenegraph
I doubt it can be done portably, but are there any solutions out there?
I don't think this can be done cleanly, but I'll ask anyway. I have
Why are pipes considered dangerous to use? What can be done to avoid these
Can this be done by setting a property? I'd prefer that approach then to
Can this be done w/ linqtosql? SELECT City, SUM(DATEDIFF(minute,StartDate,Completed)) AS Downtime FROM Incidents GROUP
Has anyone seen this before - and can anything be done about it? This
What is currying? How can currying be done in C++? Please Explain binders in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.