473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 "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.  
Oct 26 '08 #1
2 2522
joedeene
583 512MB
Let me know if this similar thread helps you.

joedeene
Oct 26 '08 #2
tlhintoq
3,525 Expert 2GB
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
Oct 27 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kepler | last post by:
I have a situation where I need to use a Using statement that creates some records in a database. After that completes, if it completes, I need to do some file creation. Any code I'm putting...
4
by: SteadySteps | last post by:
Hi I migrated a project which compiles correctly on VC 6.0 to VS 2002. However now all I get several warning that all the statements within catch blocks are "unreachable code". How can I correct...
6
by: dfetrow410 | last post by:
Can I do this in an if statement? public string getClass() { counta = counta + 1; if (counta < 2 ) {
3
by: Don Burden | last post by:
We've started converting some applications to the .NET 2.0 framework. When compiling in VS 2005, I'm getting a warning on this line: return (unitWidth != null ) ? unitWidth : new Unit("0px"); ...
8
by: teddysnips | last post by:
I'm new to C# - recent background mainly ASP.NET with VB.NET. Anyhoot, I needed to create a C# statement analogous to VB's IIf: VB.NET Dim e As Boolean e = IIf((CInt(MyVariable) 0), True,...
3
by: cie | last post by:
Hi, anyone please help me when I try to close a connection to the database from a static method, "Unreachable code detected" error occured. What's the problem actually? Thank you before
1
by: ITC | last post by:
When i write web method in C#. I got a warning and can't get the right result though it run. Code is below. public string CurrencyConverter(double amt, string fromCurrency, string...
1
by: HillBilly | last post by:
What does that error mean and why would each break statement be marked as unreachable? It implies the return will --always-- return some --thing-- if even null and no further processing can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.