3

I am a beginner in learning c# (and any coding language) I am trying to use switch statement instead of if else.

this is the working if else statement

  private void RunScript(int a, int b, ref object A)
  {
    if (a < b)
    {
      Print("a is smaller than b");
      Print("b is bigger than a");
    }
    else if (a > b)
    {
      Print("a is bigger than b");
      Print("b is smaller than a");
    }
    else
    {
      Print("a equals b");
    }

this is the switch that I am trying to do

 private void RunScript(double a, double b, ref object A)
      {
        double whichIsBigger = a - b;
//below is the 58th line
        switch (whichIsBigger)
        {
          case whichIsBigger < 0:
            Print("a is bigger than b");
            break;
          case whichIsBigger > 0:
            Print("a is smaller than b");
            break;
          default:
            Print("a equals b");
            break;
        }

It gives me this Error (CS0151): A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type (line 58)

FYI, I'm trying to do this on rhinoceros3d, using the rhino common library.

and also, I've been trying to find a website or forum to learn c# where I can ask questions like these. I ended up here. I think that this kind of questions is pretty basic, but I can't find a resource that can give me an answer to this problem. I have read several posts and can't find a similar problem

If there are any sites where people can answer my questions fast like a chat room or something, please do let me know.

Arceps
  • 39
  • 1
  • 2
    Microsoft has some great [documentation](https://learn.microsoft.com/en-us/dotnet/csharp/index) for C#. Especially for the [Switch Statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch). :) – Jaskier Nov 20 '18 at 17:59
  • https://www.dotnetperls.com/ is another great resource with examples for C# programming fundamentals. – Sam W Nov 20 '18 at 18:05

3 Answers3

4

Basically, you're trying to run an evaluation in your case statement. You have to do the evaluation before, and use the values in your case statement.

If it's a true / false situation, you shouldn't use switch. Switch is generally for when there are a number of options that could be true. For example, if you had an enum with multiple values, and you want to do something different for each value (like DayOfWeek.Monday, DayOfWeek.Tuesday, etc). For the exact reason you're running into here.

If you really wanted, you could create an enum of ABCompare.Bigger, ABCompare.Smaller, ABCompare.Equal or something like that, and then switch on that -- but that doesn't really make sense.

Jonathan
  • 4,916
  • 2
  • 20
  • 37
1

The switch statement works by comparing the value you pass in to a list of alternatives you provide. So, you can do:

switch (a < b)
{
    case true:
        // do some stuff
        break;
    case false:
        switch (a > b)
        {
            case true:
                // do other stuff
                break;
            case false:
                // do other other stuff
                break;
        }
        break;
 }

but you can't do direct comparisons in the case statement because they're already doing a comparison with the value you passed into the original switch.

Also, the afore-mentioned example is a poor use case for switch as it would be better-handled by an if-else. If your goal is to understand switch, my advice would be to try converting an enum to some other type based on its values:

public enum Color
{
    Red,
    Blue,
    Green,
}

public string ConvertToHexWithIfElse(Color myColor)
{
    if (myColor == Color.Red)
    {
        return "#FF0000";
    }
    else if (myColor == Color.Green)
    {
        return "#00FF00";
    }
    else if (myColor == Color.Blue)
    {
        return "#0000FF";
    }

    return string.Empty;
}

public string ConvertToHexWithSwitch(Color myColor)
{
    switch (myColor)
    {
        case Color.Red:
            return "#FF0000";
        case Color.Blue:
            return "#0000FF";
        case Color.Green:
            return "#00FF00";
        default:
            return string.Empty;
    }
}

Note that even this example is somewhat of a poor use of switch because the enum was a forced contrivance used simply to show the usage. IMHO switch doesn't have many actual uses: you either use a dictionary or you use an if-else.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • I mean... ok this answers the actual question but doing this in practice is NOT recommended. – BradleyDotNET Nov 20 '18 at 18:10
  • @BradleyDotNET Realized that so I added a _proper_ use of it – Woody1193 Nov 20 '18 at 18:13
  • 1
    To your last comment, `switch` is very useful when you have some actual logic based on the case (instead of just converting to a value as in your contrived example). You can *always* replace with an if/else but that is more verbose. A dictionary, can be used for the logic case but the syntax is awkward. – BradleyDotNET Nov 20 '18 at 18:22
  • @BradleyDotNET Agreed. If there's a function conversion then use that, if there's a type-uniformity and no logic then a dictionary is better and if there are few cases then an if-else would make more sense. The only time I've found I needed a switch case was when working with objects that were something like "I want a value in these three cases but for these other four I need to make method calls" or something similar. Think state machines or automata – Woody1193 Nov 20 '18 at 18:32
  • 1
    Yep, state machines are a classic example (though to be fair I've typically been better served by the State pattern for those) – BradleyDotNET Nov 20 '18 at 18:35
  • @BradleyDotNET Also agree – Woody1193 Nov 20 '18 at 18:37
  • ah, so I'm forcing a function in an improper way. I'm trying to figure out how if else and switch differs. I was confused because I read somewhere that I should use switch statement if the condition is more than 2 (if - else) just because it is more readable. I get it now, thanks a lot guys. – Arceps Nov 20 '18 at 23:23
1

When doing a switch statement each "case" is not supposed to have a conditional in it. Switch statements are designed to "switch" values. Like for example, swapping colors!

Color c = (Color) (new Random()).Next(0, 3);
switch (c)
{
    //Value of "c" is red
    case Color.Red:
       Console.WriteLine("Red!");
       break;
    //Value of "c" is green
    case Color.Green:
       Console.WriteLine("Green!");
       break;
    //Value of "c" is blue
    case Color.Blue:
       Console.WriteLine("Blue!");   
       break;
    //"c" is not red, green, or blue, so we default our message to say the color is unknown!
    default:
       Console.WriteLine("The color is not known.");
       break;   
}

In each "case" we see if "c" is a specific value, and if not, we have a default in our switch statement to handle the scenario.

Ostrava
  • 95
  • 1
  • 9