Unreachable code detected in case statement

前端 未结 14 2451
终归单人心
终归单人心 2020-12-06 16:37

I have a code:

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


        
14条回答
  •  自闭症患者
    2020-12-06 17:11

    As the code is currently, the "break" commands will never be run nor will the final "return true" command. Removing these would get rid of the warnings.

    You might want to try for a solution with fewer return paths as it can make code harder to debug and understand. Something like this:

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool processed = false;
            switch (keyData)
            {
                case Keys.Alt | Keys.D1:
    
                    if (this._condition1)
                        processed = true;
    
                    break;
    
                case Keys.Control | Keys.U:
    
                    if (this._condition2)
                        processed = true;
    
                    break;
            }
    
            if (!processed)
                processed = base.ProcessCmdKey(ref msg, keyData);
    
            return processed;
        }
    

提交回复
热议问题