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 728075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:38:24+00:00 2026-05-14T06:38:24+00:00

I have a code: protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch

  • 0

I have a code:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Alt|Keys.D1:

                if (this._condition1)
                {
                    return true;
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }

                break;

            case Keys.Control |Keys.U:

                if (this._condition2)
                {
                    return true;
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }

                break;

            default:

                return base.ProcessCmdKey(ref msg, keyData);
        }

        return true;

It gives me “unreachable code detected” warning on breaks.

Is it good practice not to use break operator here ? I don’t want to turn off “unreachable code detected” warning.

PS: There are many case in my ProcessCmdKey method.

  • 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-14T06:38:24+00:00Added an answer on May 14, 2026 at 6:38 am

    There are three unreachable statements in your code, first two are the break statements and the last one int he last line "return true" is also unreachable, I dont know whether C# compiler detects that or not, but logically there is no way last return statement will also be reached.

    There are multiple ways to solve this issue,

    1. Store a temp variable, called bool retVal, keep retVal and break your switch case and at the end of function return retVal.
    2. If you return value before break, break statement is useless.

    Better Design Way

    If you return values within switch cases, it may be difficult to analyze your code later on by you or someone else, usually it is better to keep a return value temp variable and return it at end of function, that becomes easier to debug and understand the code for new coder.

    Switch can be complicated, and more returns within switch may not have better control, if you want to implement logging, debugging, returns from cases could be complicated. And it becomes way to difficult browsing the logic flow graphs.

    So it is better to avoid return altogather from case, but still it depends on situtations as well, one needs to make an intelligent decision here.

    ** Design Recommendations **
    After probably 13 years since original answer, I have found out following design guideline.

    Switch should not be mixed with any other switch or for or any other control flow statement.

    Switch should always be a separate method and it should only do selection based on the input and return something.

    Reason being, if you want to analyze results or cache results in map, or you may want to change the logic from switch to function pointers or something else, it should not change rest of the code.

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

Sidebar

Related Questions

I have code that overrides the TextBox ProcessCmdKey method: protected override bool ProcessCmdKey(ref Message
I have this code running in my onDestroy function: @Override protected void onDestroy() {
I have onPause in activity and works great. This is code @Override protected void
I have a method in C# 4.0 that is like this: protected override bool
I have the following code: int a = 0; protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
I have code like this protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType
In c# I am using this code to imediately react on press keys. protected
I have code like the following in a UserControl: Protected Overrides Sub Render(ByVal writer
I have a code using protected field in super class. How can I avoid
I have the following code: protected function safePath($path) { $path = (string) $path; $path

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.