VB6/VB.NET Case Is < 0 in c# ???
-
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
-
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
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 -
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
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 -
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
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 -
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 GandhiAh, 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 -
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 -
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 GandhiThanks everyone. So switch/case does not accept conditions... i need to do if/else for it! Thanks a lot. Regards, Chua Wen Ching :p
-
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 blogyou 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 :)
-
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 :)
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 -
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
Why don't you use switch ()