mardi 20 novembre 2018

Trying to use switch statement insted of if else to find out which value is bigger or smaller

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.

Aucun commentaire:

Enregistrer un commentaire