Connecting Tech Pros Worldwide Forums | Help | Site Map

Unreachable code detected

Newbie
 
Join Date: Oct 2008
Posts: 1
#1: Oct 26 '08
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...



Expand|Select|Wrap|Line Numbers
  1.  //Calculate Amount Discount with conditions
  2.  
  3.                     amountDiscountDecimal = 0;
  4.  
  5.  
  6.                     if (singleUserRadioButton.Checked)
  7.                     {
  8.                         switch ("enterAmountDecimal")
  9.                         {
  10.                             case " >= 15":
  11.  
  12.                                 amountDiscountDecimal = Decimal.Round(
  13.                                 (amountPurchasedDecimal * SINGLE_USER_DISCOUNT_RATE_Decimal), 2);
  14.                                 break;
  15.  
  16.                             case " < 15":
  17.  
  18.                                 amountDiscountDecimal = 0;
  19.                                 break;
  20.                         }
  21.                     }
  22.                     else if (multipleUsersRadioButton.Checked)
  23.                     {
  24.                         if (numberOfUsersInteger >= 3)
  25.                         {
  26.                             switch ("enterAmountDecimal")
  27.                             {
  28.                                 case " >= 45":
  29.                                     amountDiscountDecimal = Decimal.Round(
  30.                                         (amountPurchasedDecimal * MULTIPLE_USERS_DISCOUNT_RATE_Decimal), 2);
  31.                                     break;
  32.                                 case " < 45":
  33.                                     amountDiscountDecimal = 0;
  34.                                     break;
  35.                             }
  36.                         }
  37.                         else
  38.                         {
  39.                             //Display error message if users are less than 3
  40.                             MessageBox.Show("Number of users must be more than 3.", "Data Entry Error");
  41.                             numberOfUsersTextBox.Focus();
  42.                             numberOfUsersTextBox.SelectAll();
  43.                         }
  44.  
  45.                     }
  46.                     else
  47.                     {
  48.                             //display error message if no user type is selected
  49.                             MessageBox.Show("Please select user type and enter amount to calculate discount.", "Required Entry");
  50.  
  51.                     }
  52.  

joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#2: Oct 26 '08

re: Unreachable code detected


Let me know if this similar thread helps you.

joedeene
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#3: Oct 27 '08

re: Unreachable code detected


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...



Expand|Select|Wrap|Line Numbers
  1.  //Calculate Amount Discount with conditions
  2.  
  3.                     amountDiscountDecimal = 0;
  4.  
  5.  
  6.                     if (singleUserRadioButton.Checked)
  7.                     {
  8.                         switch ("enterAmountDecimal")
  9.                         {
  10.                             case " >= 15":
  11.  
  12.                                 amountDiscountDecimal = Decimal.Round(
  13.                                 (amountPurchasedDecimal * SINGLE_USER_DISCOUNT_RATE_Decimal), 2);
  14.                                 break;
  15.  
  16.                             case " < 15":
  17.  
  18.                                 amountDiscountDecimal = 0;
  19.                                 break;
  20.                         }
  21.                     }
  22.                     else if (multipleUsersRadioButton.Checked)
  23.                     {
  24.                         if (numberOfUsersInteger >= 3)
  25.                         {
  26.                             switch ("enterAmountDecimal")
  27.                             {
  28.                                 case " >= 45":
  29.                                     amountDiscountDecimal = Decimal.Round(
  30.                                         (amountPurchasedDecimal * MULTIPLE_USERS_DISCOUNT_RATE_Decimal), 2);
  31.                                     break;
  32.                                 case " < 45":
  33.                                     amountDiscountDecimal = 0;
  34.                                     break;
  35.                             }
  36.                         }
  37.                         else
  38.                         {
  39.                             //Display error message if users are less than 3
  40.                             MessageBox.Show("Number of users must be more than 3.", "Data Entry Error");
  41.                             numberOfUsersTextBox.Focus();
  42.                             numberOfUsersTextBox.SelectAll();
  43.                         }
  44.  
  45.                     }
  46.                     else
  47.                     {
  48.                             //display error message if no user type is selected
  49.                             MessageBox.Show("Please select user type and enter amount to calculate discount.", "Required Entry");
  50.  
  51.                     }

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
Reply