Quote:
Originally Posted by selasebytes
how do i get over this warning to get my program running correctly??
Below is the faulty part of the code. Am trying to calculate discount on a purchase form. the codes for my discount is nested in an if statement with a switch case involved. how ever upon executing, my form dosent calculate the discount due to the warning that says "unreachable code detected". this code i guess is the calculation for my discount with the case declaration.
please help...
-
//Calculate Amount Discount with conditions
-
-
amountDiscountDecimal = 0;
-
-
-
if (singleUserRadioButton.Checked)
-
{
-
switch ("enterAmountDecimal")
-
{
-
case " >= 15":
-
-
amountDiscountDecimal = Decimal.Round(
-
(amountPurchasedDecimal * SINGLE_USER_DISCOUNT_RATE_Decimal), 2);
-
break;
-
-
case " < 15":
-
-
amountDiscountDecimal = 0;
-
break;
-
}
-
}
-
else if (multipleUsersRadioButton.Checked)
-
{
-
if (numberOfUsersInteger >= 3)
-
{
-
switch ("enterAmountDecimal")
-
{
-
case " >= 45":
-
amountDiscountDecimal = Decimal.Round(
-
(amountPurchasedDecimal * MULTIPLE_USERS_DISCOUNT_RATE_Decimal), 2);
-
break;
-
case " < 45":
-
amountDiscountDecimal = 0;
-
break;
-
}
-
}
-
else
-
{
-
//Display error message if users are less than 3
-
MessageBox.Show("Number of users must be more than 3.", "Data Entry Error");
-
numberOfUsersTextBox.Focus();
-
numberOfUsersTextBox.SelectAll();
-
}
-
-
}
-
else
-
{
-
//display error message if no user type is selected
-
MessageBox.Show("Please select user type and enter amount to calculate discount.", "Required Entry");
-
-
}
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. You can also just type the codes yourself.
More on tags. They're cool. Check'em out.
Take a close look at lines 8 and 26. You are trying to switch based on a string. Not a variable, but an actual string because it is in quotes.
Then also look at 10, 16, 28, 32. It is completely legal to have case statement looking for strings. But those strings are not taken like formulas to be evaluated. Assuming you fixed line 26 to look at the variable 'enterAmoundDecimal' and not at the string ' "enterAmountDecimal" ' that variable would have to contain the actual string " < 45" or " >45" for either of the two case statement to run. It wouldn't evaluate the string as a formula to (enterAmountDecimal<45)
Good Switch example The MSDN explanation of Switch operator
Regards,
tlhIn'toQ