Tuesday, March 13, 2012

switch

Hi
Is it possible to replace this if statement by a switch?
private int doCalculation(int lhs, int rhs)
{
int result = 0;

if (addition.Checked)
result = addValues(lhs, rhs);
else if (subtraction.Checked)
result = subtractValues(lhs, rhs);
else if (multiplication.Checked)
result = multiplyValues(lhs, rhs);
else if (division.Checked)
result = divideValues(lhs, rhs);
else if (remainder.Checked)
result = remainderValues(lhs, rhs);

return result;
}

thanksI'm afraid not. You could use a ternary operator instead, but you're essentially doing the same thing as the if statements:

private int doCalculation(int lhs, int rhs)
{
return (addition.Checked) ? addValues(lhs, rhs) :
(subtraction.Checked) ? subtractValues(lhs, rhs) :
(multiplication.Checked) ? multiplyValues(lhs, rhs) :
(division.Checked) ? divideValues(lhs, rhs) :
(remainder.Checked) ? remainderValues(lhs, rhs) : 0;
}

i don't think so in C#

the shared thing in these Values is true Value
so, you will use it to compare it by all checked value for RadioButtons But C# Dont Agree but in case CONSTANT it agree for values only

But in VB.NET You Can Do That


Select Case True
Case addition.Checked

Case subtraction.Checked

Case multiplication.Checked

Case division.Checked

Case remainder.Checked

End Select


I can't either find out an answer because I think each if statement tests a different condition.

thanks for comments

0 comments:

Post a Comment