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

I want to better understand the capabilities and limitations of what can be done
What can be done to resolve the exception: CrystalDecisions.CrystalReports.Engine.InternalException: Failed to open a rowset.
it can be done using javascript, but with CSS alone, is it possible to
What can be done in Cpython that can not be done in IronPython?
What can be done about negative numbers in library item counts? ItemCount is a
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 know this can be done and i have seen it done using some
I know it can be done but am having issues getting it to work.
I don't think this can be done cleanly, but I'll ask anyway. I have

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.