user3772108
user3772108

Reputation: 874

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;
}

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

Upvotes: 2

Views: 3364

Answers (5)

Stefan Zvonar
Stefan Zvonar

Reputation: 4319

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_HOTKEY)
    {
        var param = (int)m.WParam;
        switch (param)
        {
            case 1:
                Console.WriteLine("w");
                break;
            case 2:
                Console.WriteLine("a");
                break;
            case 3:
                Console.WriteLine("s");
                break;
            case 4:
                Console.WriteLine("d");
                break;
            default:
                Console.WriteLine("Unrecognised key stroke.");
        }
        base.WndProc(ref m);
    }
    // todo:  What if m.Msg is no WM_HOTKEY?
}

Upvotes: 0

Christos
Christos

Reputation: 53958

Since the value of Msg is always the same (in your conditions), you could try this:

if(m.Msg == WM_HOTKEY)
{
    switch ((int)m.WParam)
    {
        case 1:
            Console.WriteLine("w");
            break;
        case 2:
            Console.WriteLine("a");
            break;
        case 3:
            Console.WriteLine("s");
            break;
        case 4:
            Console.WriteLine("d");
            break;
    }
}

It is important to realize the following (taken from MSDN) regarding the switch statement:

Each case label specifies a constant value. The switch statement transfers control to the switch section whose case label matches the value of the switch expression (caseSwitch in the example). If no case label contains a matching value, control is transferred to the default section, if there is one. If there is no default section, no action is taken and control is transferred outside the switch statement.

Upvotes: 4

Verbon
Verbon

Reputation: 545

The only thing you can do here is this:

if(m.Msg == WM_HOTKEY)
{
    var param = (int)m.WParam;
    switch(param)
    {
        case 1:
            Console.WriteLine("w");
            break;
            ....
    }
}

Upvotes: 0

Dmitry
Dmitry

Reputation: 14059

You cannot switch on the condition. You can only switch on constant values.

if (m.Msg == WM_HOTKEY)
{
    switch ((int)m.WParam)
    {
        case 1:
            Console.WriteLine("w");
            break;
        case 2:
            Console.WriteLine("a");
            break;
        case 3:
            Console.WriteLine("s");
            break;
        case 4:
            Console.WriteLine("d");
            break;
    }
}

Upvotes: 4

gallickgunner
gallickgunner

Reputation: 480

No you can't use logical operators in switch case. It operates on only a single value

Upvotes: 1

Related Questions