8

So I was making a Rock Paper Scissor game and I've sort of made adjustments to it to include life and other things. Now I got stuck with the switch statement. My if statement works fine:

private void Result_TextChanged(object sender, EventArgs e)
{
     if (playerscore == 1 || pcscore == 1)
     {
          PlayerLife.Image = Properties.Resources.Five;
     }
}

I was wondering how I could translate this to the switch statement?

private void Result_TextChanged(object sender, EventArgs e)
{              
    switch (playerscore || pcscore)
    {
         case 1:
             PlayerLife.Image = Properties.Resources.Five;
             break;
    }
}

Doesn't seem to work.

Harrison
  • 3,843
  • 7
  • 22
  • 49
user2911044
  • 117
  • 1
  • 2
  • 4
  • You have to `switch` on a single variable's value, and why do you want to use switch? – Vishal Oct 23 '13 at 16:31
  • No that's not possible in C#. VB.NET might allow something like that though, because its [`Select` statement](http://msdn.microsoft.com/en-us/library/cy37t14y.aspx) is a lot more flexible than C#'s `switch`. – p.s.w.g Oct 23 '13 at 16:31
  • May I ask why an `if` statement is not appropriate in this scenario? – clcto Oct 23 '13 at 16:34
  • case is a way of selecting a bracket of code to run based on the VALUE of a given switch condition. Usually this will be a variable. In your case, using AND / OR will always only result in one of two possible values, true or false. So case is not appropriate here. – Scott Solmer Oct 23 '13 at 16:34
  • If you are coming from JavaScript where `||` used to pick non-falsy value than C# equivalent of JavaScript `||` is approximately `playerscore != 0? playerscore:pcscore`, or sometimes `??` for reference types (http://stackoverflow.com/questions/19534876/null-coalescing-operator-in-accessor-method#comment28982817_19534876) – Alexei Levenkov Oct 23 '13 at 16:41
  • If you want only one `switch` section, why use `switch`? An `if` is better. Suppose you have more than one `case`, and suppose it was allowed to `switch` on "`x` OR `y`", then if `x` was `1` and `y` was `2`, would you expect both `case 1` and `case 2` to run? In what order? – Jeppe Stig Nielsen Oct 23 '13 at 16:50

7 Answers7

11

The simple answer is No. You cant use it like that.

Switch works with single expression.

You may check MSDN for details.

You may try like this:-

  if (playerscore == pcscore)
        {
            switch (playerscore)
            {
                case 1:
                PlayerLife.Image = Properties.Resources.Five;
                break;
            }
        }

EDIT:-

As commented by Jeppe Stig Nielsen in the comments, You can switch on any expression of a suitable type. That expression may contain ||. There can be many case labels associated with each switch section in a switch block.

But personally speaking that would not be a good practice to follow. You may try to use if statement for that.

You may try like this if you want:

 switch (playerscore == 1 || pcscore == 1)
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Oh okay. So if I wanted to make it a switch statement I have to separate the playerscore and pcscore? – user2911044 Oct 23 '13 at 16:32
  • 1
    @user2911044:- I think it would be better if you can use if statement for that. As a suggestion dont violate the rules of C#. – Rahul Tripathi Oct 23 '13 at 16:34
  • 1
    Downvote. You can `switch` on any expression of a suitable type. That expression may contain `||`. There can be many `case` labels associated with each switch section in a switch block. – Jeppe Stig Nielsen Oct 23 '13 at 16:38
  • @JeppeStigNielsen:- Yes I agree Sir but I dont think that would be a great practice to follow! Do correct me if I am wrong:) – Rahul Tripathi Oct 23 '13 at 16:39
  • I wrote this `if`/`switch` combo too, but then realized it's supposed to be `playerscore == 1 || pcscore == 1`, not `playerscore == 1 && pcscore == 1`, so that isn't right. Edit: Oh, but the title asked about both, so that'd work for the `&&` case only. – Tim S. Oct 23 '13 at 16:41
  • Well, regarding the situation the asker shows, I can't see a useful way to use `switch` for that. But in another situation you could use `switch (playerScore > pcScore || isGameOver ? pcScore : playerScore) { ... }` or whatever. – Jeppe Stig Nielsen Oct 23 '13 at 16:43
  • 1
    +1. I think that "single variable value" is confusing, and "single value" is better (like `playerscore | pcscore` would be fine, but 2 variables) – Alexei Levenkov Oct 23 '13 at 16:43
  • If you change *Switch works with single value.* to *Switch works with single expression.* I'll give you an upvote. – Mike Christensen Oct 23 '13 at 16:47
3

In C#, a switch statement resolves a single expression and compares that value with a list of possible cases:

switch(someExpression)
{
   case x: // This runs if someExpression == x
      break;
   case y: // This runs if someExpression == y
      break;
}

Now, you could switch on the expression (playerscore == 1 || pcscore == 1) like so:

switch(playerscore == 1 || pcscore == 1) // This expression is either true or false
{
   case true: // Runs if playerscore is 1 or pcscore is 1
      break;
   case false: // runs if neither playscore or pcscore are 1
      break;
}

However, the above is rather unreadable and silly. You'd be best off with the if statement:

if(playerscore == 1 || pcscore == 1)
{
   // Runs if playerscore is 1 or pcscore is 1
}
else
{
   // runs if neither playscore or pcscore are 1
}
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
3

You could write it like this but why would you want to?

  switch (playerscore == 1 || pcscore == 1)
        {
            case true:
                PlayerLife.Image = Properties.Resources.Five;
                break;
            default:
                break;
        }

As Jeppe points out in the comment below, when you use || or && you end up with a bool and an if statement should be used.

Here is a great answer by @EricLippert on what can be used as the expression in a swtich statement.

Community
  • 1
  • 1
Harrison
  • 3,843
  • 7
  • 22
  • 49
1

What you are trying to do doesn't make sense, if playerscore = 3 and pcscore = 2 then what would playerscore || pcscore be equal to?

Jonathan
  • 21
  • 3
1

If you have a whole bunch of variables, say not just two, but 5, 10, or even an unknown number, then what you can do is put all of the values that you want to compare to 1 into a collection and then act on that collection as a whole.

//this could just be a list/array accepted as a paramter, 
//can include other variables, or whatever
var scores = new []{playerscore, pcscore};

if(scores.Any(score => score == 1))
    PlayerLife.Image = Properties.Resources.Five;

switch isn't really an appropriate tool for manipulating collections like this.

Servy
  • 202,030
  • 26
  • 332
  • 449
0

This makes no sense: in a switch statement you always want to compare with a specific type, rather than to a boolean value as follows:

switch (playerscore || pcscore)

in your case use the 'if'-statement

rudy
  • 185
  • 2
  • 11
0

Suppose that playerscore and pcscore are integer who has 0 or 1 as possible values

    resp = playerscore + 10 * pcscore;
    switch (resp)
    {
        case 0:
            // both are false
            break;
        case 1:
            // playerscore true
            break;
        case 10:
            // pcscore true
            break;
        case 11:
            // both are true
            break;
        default:
            // error in input data
            break;
    }
Federico
  • 469
  • 2
  • 15