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

  • Home
  • SEARCH
  • 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 519955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:05:41+00:00 2026-05-13T08:05:41+00:00

I have code very similar to this example three times in a code behind.

  • 0

I have code very similar to this example three times in a code behind.
Each time the switch is toggling off of an option that is sent to it. Each
time the code inside the case is exactly the same except for a parameter
based off of the case. Is using a switch/case and methods the best way
to do this? Should I think of using sometype of design pattern to avoid the repeative switch/case structure?

string option = dropDownList.SelectedValue.ToString();
switch (option.ToUpper())
{
    case "ALPHA":
        // do repeative code method here; only change is a parameter
        break;
    case "BRAVO":
        // do repeative code method here; only change is a parameter
        break;
    case "CHARLIE":
        // do repeative code method here; only change is a parameter
        break;
    case "DELTA":
        // do repeative code method here; only change is a parameter
        break;
    default:
        break;
}
  • 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-13T08:05:41+00:00Added an answer on May 13, 2026 at 8:05 am

    Compilers are very good at optimizing switch/case constructs; the CLR will likely turn it into a lookup table or something similarly fast, so hand-rolling your own version such as Henk Holterman suggests is not what I would recommend. The CLR can do a better job than you can at choosing the best algorithm.

    If it’s an issue of elegance or maintainability, and you have several switch/cases strewn about the same class performing similar functions, then one way to improve it is to encapsulate all of the functionality related to a single “case” into its own class instance, like so:

    class MyOption
    {
        public static readonly MyOption Alpha = new MyOption(1, 10, "Alpha Text");
        public static readonly MyOption Bravo = new MyOption(2, 100, "Bravo Text");
        public static readonly MyOption Charlie = new MyOption(3, 1000, "Charlie Text");
        // ... Other options ...
        public static readonly MyOption Default = new MyOption(0, 0, null);
    
        public MyOption(int id, int value, string text)
        {
            this.ID = id;
            this.Value = value;
            this.Text = text;
        }
    
        public int ID { get; private set; }
        public int Value { get; private set; }
        public string Text { get; private set; }
    }
    

    Then in your class/control/page:

    static MyOption GetOption(string optionName)
    {
        switch (optionName)
        {
            case "ALPHA":
                return MyOption.Alpha;
            case "BRAVO":
                return MyOption.Bravo;
            case "CHARLIE":
                return MyOption.Charlie;
            // ... Other options ...
            default:
                return MyOption.Default;
        }
    }
    
    private MyOption GetOptionFromDropDown()
    {
        string optionName = GetOptionNameFromDropDown();
        return GetOption(optionName);
    }
    
    private string GetOptionNameFromDropDown()
    {
        // ... Your code ...
    }
    

    After that you can start churning out events and other methods:

    private void control1_SomeEvent(object sender, EventArgs e)
    {
        MyOption option = GetOptionFromDropDown();
        DoSomething(option.ID);
    }
    
    private void control2_SomeEvent(object sender, EventArgs e)
    {
        MyOption option = GetOptionFromDropDown();
        DoSomethingElse(option.Value);
    }
    

    Of course, this is only a useful pattern if you have several of these switch/cases that you want to refactor into one. If you’ve only got one switch/case, you’re just going to end up with a lot more code this way, so leave it alone!

    Other possibilities to improve maintainability include:

    • Changing the string to an Enum type (convert optionName using Enum.Parse);
    • Moving all the MyOption/GetOption stuff into its own class (if you have several classes/controls/pages that all need to operate on the same set of choices);
    • Add a method delegate to the MyOption class if you actually need to call a different method for each;
    • Have your DropDownList or other control store a direct reference to the MyOption instance, if possible.

    That’s about it. It’s simple to write, it’s easy to understand, it’s easy to maintain, it will save you time if you have a lot of switch/case constructs, and it still allows the CLR to perform the best possible optimizations. The only cost is the small amount of memory required to hold those readonly fields.

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

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The crypt(3) man page describes it in the NOTES section. May 13, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer I would recommend using a dedicated template: <!-- check if… May 13, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer You're actually on the right track with the multiple repository… May 13, 2026 at 7:38 pm

Related Questions

I've got some code which draws data from an xml file but it seems
I'm asking a question very similar to this one —dare I say identical? An
Html can contain little bits of Javascript embedded in it (e.g. defined in onclick
I have the following code in the OnExecute of a TIdTCPServer (Delphi 2009 and
Firstly, I realise that this is a very similar question to this one: Which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.