Unreachable code detected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selasebytes
    New Member
    • Oct 2008
    • 1

    #1

    Unreachable code detected

    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 "unreachabl e code detected". this code i guess is the calculation for my discount with the case declaration.
    please help...



    [code=c#]
    //Calculate Amount Discount with conditions

    amountDiscountD ecimal = 0;


    if (singleUserRadi oButton.Checked )
    {
    switch ("enterAmountDe cimal")
    {
    case " >= 15":

    amountDiscountD ecimal = Decimal.Round(
    (amountPurchase dDecimal * SINGLE_USER_DIS COUNT_RATE_Deci mal), 2);
    break;

    case " < 15":

    amountDiscountD ecimal = 0;
    break;
    }
    }
    else if (multipleUsersR adioButton.Chec ked)
    {
    if (numberOfUsersI nteger >= 3)
    {
    switch ("enterAmountDe cimal")
    {
    case " >= 45":
    amountDiscountD ecimal = Decimal.Round(
    (amountPurchase dDecimal * MULTIPLE_USERS_ DISCOUNT_RATE_D ecimal), 2);
    break;
    case " < 45":
    amountDiscountD ecimal = 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");
    numberOfUsersTe xtBox.Focus();
    numberOfUsersTe xtBox.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");

    }
    [/code]
  • joedeene
    Contributor
    • Jul 2008
    • 579

    #2
    Let me know if this similar thread helps you.

    joedeene

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      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 "unreachabl e code detected". this code i guess is the calculation for my discount with the case declaration.
      please help...



      [CODE=c]
      //Calculate Amount Discount with conditions

      amountDiscountD ecimal = 0;


      if (singleUserRadi oButton.Checked )
      {
      switch ("enterAmountDe cimal")
      {
      case " >= 15":

      amountDiscountD ecimal = Decimal.Round(
      (amountPurchase dDecimal * SINGLE_USER_DIS COUNT_RATE_Deci mal), 2);
      break;

      case " < 15":

      amountDiscountD ecimal = 0;
      break;
      }
      }
      else if (multipleUsersR adioButton.Chec ked)
      {
      if (numberOfUsersI nteger >= 3)
      {
      switch ("enterAmountDe cimal")
      {
      case " >= 45":
      amountDiscountD ecimal = Decimal.Round(
      (amountPurchase dDecimal * MULTIPLE_USERS_ DISCOUNT_RATE_D ecimal), 2);
      break;
      case " < 45":
      amountDiscountD ecimal = 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");
      numberOfUsersTe xtBox.Focus();
      numberOfUsersTe xtBox.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");

      }
      [/CODE]

      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 'enterAmoundDec imal' and not at the string ' "enterAmountDec imal" ' 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 (enterAmountDec imal<45)

      Good Switch example
      The MSDN explanation of Switch operator

      Regards,
      tlhIn'toQ

      Comment

      Working...