dimanche 7 février 2016

C# switch case instead of if else

How is it possible to switch this code into a switch case?

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_HOTKEY && (int)m.WParam == 1)
            Console.WriteLine("w");
        else if (m.Msg == WM_HOTKEY && (int)m.WParam == 2)
            Console.WriteLine("a");
        else if (m.Msg == WM_HOTKEY && (int)m.WParam == 3)
            Console.WriteLine("s");
        else if (m.Msg == WM_HOTKEY && (int)m.WParam == 4)
            Console.WriteLine("d");

        base.WndProc(ref m);
    }

This is my last try and the line on the first case is again marked as red

            switch (m.Msg)
            {
                case m.Msg == WM_HOTKEY && (int)m.WParam == 1:
                    Console.WriteLine("w");
                    break;
                case m.Msg == WM_HOTKEY && (int)m.WParam == 2:
                    Console.WriteLine("a");
                    break;
                case m.Msg == WM_HOTKEY && (int)m.WParam == 3:
                    Console.WriteLine("s");
                    break;
                case m.Msg == WM_HOTKEY && (int)m.WParam == 4:
                    Console.WriteLine("d");
                    break;
                default:
                    lblMessage.Text = "Sorry, key not supported";
                    break;
            }

Is this possible? I think it would look better to read inside a switch statement.

Aucun commentaire:

Enregistrer un commentaire