I’m trying to make a switch case with more than one variable, but I don’t know how to make it work, can anyone help me?

Asked

Viewed 48 times

-2

I want to assign these 4 variables to the switch case however when I will determine the case with the 4 variables it does not accept, says: CS0029 Cannot implicitly convert "bool" type to "(double X1, double Y1, double x2, double Y2)" Calculate distances

public class calculo
{
    public void calcular(double x1, double y1, double x2, double y2)
    {

        switch(x1, y1, x2, y2)
        {
            case x1 == x2 && y1 != y2:
                break;
        }
    }


}

1 answer

0


This is not possible to do, the switch use a single "commutator" or value that must be tested, and is always equal, one cannot combine conditions such as x1 == x2 && y1 != y2.

For example:

Random rnd = new Random();
int aleatorio = rnd.Next(1,4);

switch (aleatorio)
{
   case 1:  // significa caso aleatorio igual a 1
       Console.WriteLine("Um");
       break;
   case 2:
       Console.WriteLine("Dois");
       break;
     ....
}

I suggest you read more about How the switch works

Even using when (for example switch(x1) case 10 when x1 < y1), it would not be possible to do with switch, your example is a case that can be dealt with if, for example:

if (x1 == x2 && y1 != y2)
{
    ... comandos ...
}
  • Thank you very much, but would I have some command similar to the switch that does what I want? .

  • does not have, but can use if/Else if and keep adding conditions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.