Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. VB6/VB.NET Case Is < 0 in c# ???

VB6/VB.NET Case Is < 0 in c# ???

Scheduled Pinned Locked Moved C#
csharphelpquestionlearning
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chua Wen Ching
    wrote on last edited by
    #1

    Hi there. I need some help here. I am trying to convert a sample application for learning purposes. But i am stuck to see this: Select Case ShipPitchSM Case Is > 0 ShipPitchSM = ShipPitchSM - Friction If ShipPitchSM < 0 Then ShipPitchSM = 0 If ShipPitchSM > 0.03 Then ShipPitchSM = 0.03 Case Is < 0 ShipPitchSM = ShipPitchSM + Friction If ShipPitchSM > 0 Then ShipPitchSM = 0 If ShipPitchSM < -0.03 Then ShipPitchSM = -0.03 End Select Select Case ShipYawSM Case Is > 0 ShipYawSM = ShipYawSM - Friction If ShipYawSM < 0 Then ShipYawSM = 0 If ShipYawSM > 0.03 Then ShipYawSM = 0.03 Case Is < 0 ShipYawSM = ShipYawSM + Friction If ShipYawSM > 0 Then ShipYawSM = 0 If ShipYawSM < -0.03 Then ShipYawSM = -0.03 End Select Select Case ShipRollSM Case Is > 0 ShipRollSM = ShipRollSM - Friction If ShipRollSM < 0 Then ShipRollSM = 0 If ShipRollSM > 0.03 Then ShipRollSM = 0.03 Case Is < 0 ShipRollSM = ShipRollSM + Friction If ShipRollSM > 0 Then ShipRollSM = 0 If ShipRollSM < -0.03 Then ShipRollSM = -0.03 End Select Any help? How do you code that in c#? I try my best to place conditions in case statements, but compile errors. Thanks. Regards, Chua Wen Ching :p

    M J L 4 Replies Last reply
    0
    • C Chua Wen Ching

      Hi there. I need some help here. I am trying to convert a sample application for learning purposes. But i am stuck to see this: Select Case ShipPitchSM Case Is > 0 ShipPitchSM = ShipPitchSM - Friction If ShipPitchSM < 0 Then ShipPitchSM = 0 If ShipPitchSM > 0.03 Then ShipPitchSM = 0.03 Case Is < 0 ShipPitchSM = ShipPitchSM + Friction If ShipPitchSM > 0 Then ShipPitchSM = 0 If ShipPitchSM < -0.03 Then ShipPitchSM = -0.03 End Select Select Case ShipYawSM Case Is > 0 ShipYawSM = ShipYawSM - Friction If ShipYawSM < 0 Then ShipYawSM = 0 If ShipYawSM > 0.03 Then ShipYawSM = 0.03 Case Is < 0 ShipYawSM = ShipYawSM + Friction If ShipYawSM > 0 Then ShipYawSM = 0 If ShipYawSM < -0.03 Then ShipYawSM = -0.03 End Select Select Case ShipRollSM Case Is > 0 ShipRollSM = ShipRollSM - Friction If ShipRollSM < 0 Then ShipRollSM = 0 If ShipRollSM > 0.03 Then ShipRollSM = 0.03 Case Is < 0 ShipRollSM = ShipRollSM + Friction If ShipRollSM > 0 Then ShipRollSM = 0 If ShipRollSM < -0.03 Then ShipRollSM = -0.03 End Select Any help? How do you code that in c#? I try my best to place conditions in case statements, but compile errors. Thanks. Regards, Chua Wen Ching :p

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      if (ShipPtchSM < 0)
      {
      ...
      }
      else if (ShipPtchSM > 0)
      {
      ...
      }

      etc... Every line of code is a liability - Taka Muraoka
      A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr
      Latest AAL Article My blog

      1 Reply Last reply
      0
      • C Chua Wen Ching

        Hi there. I need some help here. I am trying to convert a sample application for learning purposes. But i am stuck to see this: Select Case ShipPitchSM Case Is > 0 ShipPitchSM = ShipPitchSM - Friction If ShipPitchSM < 0 Then ShipPitchSM = 0 If ShipPitchSM > 0.03 Then ShipPitchSM = 0.03 Case Is < 0 ShipPitchSM = ShipPitchSM + Friction If ShipPitchSM > 0 Then ShipPitchSM = 0 If ShipPitchSM < -0.03 Then ShipPitchSM = -0.03 End Select Select Case ShipYawSM Case Is > 0 ShipYawSM = ShipYawSM - Friction If ShipYawSM < 0 Then ShipYawSM = 0 If ShipYawSM > 0.03 Then ShipYawSM = 0.03 Case Is < 0 ShipYawSM = ShipYawSM + Friction If ShipYawSM > 0 Then ShipYawSM = 0 If ShipYawSM < -0.03 Then ShipYawSM = -0.03 End Select Select Case ShipRollSM Case Is > 0 ShipRollSM = ShipRollSM - Friction If ShipRollSM < 0 Then ShipRollSM = 0 If ShipRollSM > 0.03 Then ShipRollSM = 0.03 Case Is < 0 ShipRollSM = ShipRollSM + Friction If ShipRollSM > 0 Then ShipRollSM = 0 If ShipRollSM < -0.03 Then ShipRollSM = -0.03 End Select Any help? How do you code that in c#? I try my best to place conditions in case statements, but compile errors. Thanks. Regards, Chua Wen Ching :p

        J Offline
        J Offline
        J Dunlap
        wrote on last edited by
        #3

        You have to use if, elseif, and else.

        if(ShipPitchSM>0)
        {
        ShipPitchSM-= Friction;
        ShipPitchSM = Math.Max(0,ShipPitchSM);
        ShipPitchSM = Math.Min(ShipPitchSM,0.03);
        }else
        {
        ShipPitchSM += Friction;
        ShipPitchSM = Math.Min(0,ShipPitchSM);
        ShipPitchSM = Math.Max(-0.03,ShipPitchSM);
        }

        "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
        "You must be the change you wish to see in the world." - Mahatma Gandhi

        M C 2 Replies Last reply
        0
        • C Chua Wen Ching

          Hi there. I need some help here. I am trying to convert a sample application for learning purposes. But i am stuck to see this: Select Case ShipPitchSM Case Is > 0 ShipPitchSM = ShipPitchSM - Friction If ShipPitchSM < 0 Then ShipPitchSM = 0 If ShipPitchSM > 0.03 Then ShipPitchSM = 0.03 Case Is < 0 ShipPitchSM = ShipPitchSM + Friction If ShipPitchSM > 0 Then ShipPitchSM = 0 If ShipPitchSM < -0.03 Then ShipPitchSM = -0.03 End Select Select Case ShipYawSM Case Is > 0 ShipYawSM = ShipYawSM - Friction If ShipYawSM < 0 Then ShipYawSM = 0 If ShipYawSM > 0.03 Then ShipYawSM = 0.03 Case Is < 0 ShipYawSM = ShipYawSM + Friction If ShipYawSM > 0 Then ShipYawSM = 0 If ShipYawSM < -0.03 Then ShipYawSM = -0.03 End Select Select Case ShipRollSM Case Is > 0 ShipRollSM = ShipRollSM - Friction If ShipRollSM < 0 Then ShipRollSM = 0 If ShipRollSM > 0.03 Then ShipRollSM = 0.03 Case Is < 0 ShipRollSM = ShipRollSM + Friction If ShipRollSM > 0 Then ShipRollSM = 0 If ShipRollSM < -0.03 Then ShipRollSM = -0.03 End Select Any help? How do you code that in c#? I try my best to place conditions in case statements, but compile errors. Thanks. Regards, Chua Wen Ching :p

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #4

          Better yet:

          AdjustParam(ref double param, double friction)
          {
          if (param < 0)
          {
          param=param + friction;
          if (param > 0) param=0;
          if (param < -0.03) param=-0.03;
          }
          else
          if (param > 0)
          {
          param=param - friction;
          if (param < 0) param=0;
          if (param > 0.03) param=0.03;
          }
          }

          then call:

          AdjustParam(ref ShipPitchSM, Friction);
          AdjustParam(ref ShipYawSM, Friction);
          AdjustParam(ref ShipRollSM, Friction);

          and there's other optimizations you can make too. Get then sign of the parameter and you can eliminate the if-else clause. Put the bounds checking in a separate helper function. Things like that. Marc Every line of code is a liability - Taka Muraoka
          A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr
          Latest AAL Article My blog

          C 1 Reply Last reply
          0
          • J J Dunlap

            You have to use if, elseif, and else.

            if(ShipPitchSM>0)
            {
            ShipPitchSM-= Friction;
            ShipPitchSM = Math.Max(0,ShipPitchSM);
            ShipPitchSM = Math.Min(ShipPitchSM,0.03);
            }else
            {
            ShipPitchSM += Friction;
            ShipPitchSM = Math.Min(0,ShipPitchSM);
            ShipPitchSM = Math.Max(-0.03,ShipPitchSM);
            }

            "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
            "You must be the change you wish to see in the world." - Mahatma Gandhi

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #5

            Ah, even better than my example. Very nice. Marc Every line of code is a liability - Taka Muraoka
            A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr
            Latest AAL Article My blog

            J 1 Reply Last reply
            0
            • M Marc Clifton

              Ah, even better than my example. Very nice. Marc Every line of code is a liability - Taka Muraoka
              A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr
              Latest AAL Article My blog

              J Offline
              J Offline
              J Dunlap
              wrote on last edited by
              #6

              But you've got the best idea in your second post. ;)

              "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
              "You must be the change you wish to see in the world." - Mahatma Gandhi

              1 Reply Last reply
              0
              • J J Dunlap

                You have to use if, elseif, and else.

                if(ShipPitchSM>0)
                {
                ShipPitchSM-= Friction;
                ShipPitchSM = Math.Max(0,ShipPitchSM);
                ShipPitchSM = Math.Min(ShipPitchSM,0.03);
                }else
                {
                ShipPitchSM += Friction;
                ShipPitchSM = Math.Min(0,ShipPitchSM);
                ShipPitchSM = Math.Max(-0.03,ShipPitchSM);
                }

                "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                "You must be the change you wish to see in the world." - Mahatma Gandhi

                C Offline
                C Offline
                Chua Wen Ching
                wrote on last edited by
                #7

                Thanks everyone. So switch/case does not accept conditions... i need to do if/else for it! Thanks a lot. Regards, Chua Wen Ching :p

                1 Reply Last reply
                0
                • M Marc Clifton

                  Better yet:

                  AdjustParam(ref double param, double friction)
                  {
                  if (param < 0)
                  {
                  param=param + friction;
                  if (param > 0) param=0;
                  if (param < -0.03) param=-0.03;
                  }
                  else
                  if (param > 0)
                  {
                  param=param - friction;
                  if (param < 0) param=0;
                  if (param > 0.03) param=0.03;
                  }
                  }

                  then call:

                  AdjustParam(ref ShipPitchSM, Friction);
                  AdjustParam(ref ShipYawSM, Friction);
                  AdjustParam(ref ShipRollSM, Friction);

                  and there's other optimizations you can make too. Get then sign of the parameter and you can eliminate the if-else clause. Put the bounds checking in a separate helper function. Things like that. Marc Every line of code is a liability - Taka Muraoka
                  A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr
                  Latest AAL Article My blog

                  C Offline
                  C Offline
                  Csharp
                  wrote on last edited by
                  #8

                  you can easily do it with the switch statement:) ( it is really an equivalent to Select Case , just have to modify it slightly to tell if it's a + value or not, here's a simple example i made. C#:


                  private void val()
                  {
                  int x =-1;
                  switch(x.CompareTo(0)<0)
                  {
                  case true:
                  MessageBox.Show("the value is less than zero!");
                  break;
                  case false:
                  MessageBox.Show("the value is greater than zero!");
                  break;
                  }
                  }


                  hope it helps to understand the switch statement a bit more :)

                  M 1 Reply Last reply
                  0
                  • C Csharp

                    you can easily do it with the switch statement:) ( it is really an equivalent to Select Case , just have to modify it slightly to tell if it's a + value or not, here's a simple example i made. C#:


                    private void val()
                    {
                    int x =-1;
                    switch(x.CompareTo(0)<0)
                    {
                    case true:
                    MessageBox.Show("the value is less than zero!");
                    break;
                    case false:
                    MessageBox.Show("the value is greater than zero!");
                    break;
                    }
                    }


                    hope it helps to understand the switch statement a bit more :)

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #9

                    This is not exactly equivalent though. In the original code, the test is '< 0' and '> 0'. Your code is '< 0' and '>= 0', causing an inneficiency when x==0 (not that the original code was very efficient to begin with!) If you wanted to preserve the logic of the original test with a switch statement, you would need to properly qualify the test if a trinary operator:

                    switch (x < 0 ? -1 : x > 0 ? 1 : 0)
                    {
                    case -1: // < 0
                    ...
                    case 0: // == 0
                    ...
                    case 1: // > 0
                    ...
                    }

                    Hope that helps to understand how refactoring needs good test cases! :-D Marc STL, a liability factory - Anonymously
                    A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr
                    Latest AAL Article My blog

                    1 Reply Last reply
                    0
                    • C Chua Wen Ching

                      Hi there. I need some help here. I am trying to convert a sample application for learning purposes. But i am stuck to see this: Select Case ShipPitchSM Case Is > 0 ShipPitchSM = ShipPitchSM - Friction If ShipPitchSM < 0 Then ShipPitchSM = 0 If ShipPitchSM > 0.03 Then ShipPitchSM = 0.03 Case Is < 0 ShipPitchSM = ShipPitchSM + Friction If ShipPitchSM > 0 Then ShipPitchSM = 0 If ShipPitchSM < -0.03 Then ShipPitchSM = -0.03 End Select Select Case ShipYawSM Case Is > 0 ShipYawSM = ShipYawSM - Friction If ShipYawSM < 0 Then ShipYawSM = 0 If ShipYawSM > 0.03 Then ShipYawSM = 0.03 Case Is < 0 ShipYawSM = ShipYawSM + Friction If ShipYawSM > 0 Then ShipYawSM = 0 If ShipYawSM < -0.03 Then ShipYawSM = -0.03 End Select Select Case ShipRollSM Case Is > 0 ShipRollSM = ShipRollSM - Friction If ShipRollSM < 0 Then ShipRollSM = 0 If ShipRollSM > 0.03 Then ShipRollSM = 0.03 Case Is < 0 ShipRollSM = ShipRollSM + Friction If ShipRollSM > 0 Then ShipRollSM = 0 If ShipRollSM < -0.03 Then ShipRollSM = -0.03 End Select Any help? How do you code that in c#? I try my best to place conditions in case statements, but compile errors. Thanks. Regards, Chua Wen Ching :p

                      L Offline
                      L Offline
                      LovelyXiaoXinXin
                      wrote on last edited by
                      #10

                      Why don't you use switch ()

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups