9 Replies - 2798 Views - Last Post: 17 September 2012 - 07:41 PM Rate Topic: -----

#1 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

If else-if statement not executing last statement

Posted 16 September 2012 - 04:38 PM

Ok, I'm very new to C#, so don't laugh. I can't figure out why the last line of my if else-if statement does not run. The validation part of it works (for example, if I purposefully don't enter data it gives me the error message) but after I input all required data and click "Calculate" I do not get the calculation in the label field. Been racking my brain with this and I know it's probably something very simple, but I've looked at example after example of if else-if code and can't see what I'm doing wrong. Any help is appreciated.
   protected void btnCalculate_Click(object sender, EventArgs e)
    {
        double dEarnings;
        double dHealth;
        double d401k;
        int iInt = 0;
        int iHourly = 2080;
        int iWeekly = 52;
        int iMonthly = 12;
        double dAnswer;

        if (txtName.Text.Length == 0)
        {
            lblResults.Text = ("Please enter your name");
        }
        else if (txtEarnings.Text.Length == 0)
        {
            lblResults.Text = ("Please enter your earnings");
        }
        else if (double.TryParse(txtEarnings.Text, out dEarnings) == false)
        {
            lblResults.Text = ("Please enter a number");
        }
        else if (txtHealth.Text.Length == 0)
        {
            lblResults.Text = ("Please enter health deduction");
        }
        else if (double.TryParse(txtHealth.Text, out dHealth) == false)
        {
            lblResults.Text = ("Please enter a number");
        }
        else if (txt401K.Text.Length == 0)
        {
            lblResults.Text = ("Please enter 401K deduction");
        }
        else if (double.TryParse(txt401K.Text, out d401k) == false)
        {
            lblResults.Text = ("Please enter a number");
        }
        else if (rdoHourly.Checked == true)
        {
            iInt = iHourly;
        }
        else if (rdoWeekly.Checked == true)
        {
            iInt = iWeekly;
        }
        else if (rdoMonthly.Checked == true)
        {
            iInt = iMonthly;
        }
        else if ((rdoHourly.Checked == false) && (rdoWeekly.Checked == false) && (rdoMonthly.Checked == false))
        {
            lblResults.Text = "Please select an interval.";
        }
        else
        {
            dAnswer = GetTaxIncome(dHealth, d401k, dEarnings, iInt);
            lblResults.Text = "Your taxable income is " + dAnswer;
        }

    }


    double GetTaxIncome(double dEarn, double dHlth, double d401, int iInterval)
    {
        double dTaxIncome;
        dTaxIncome = dEarn * iInterval - ((dHlth + d401) * 12);
        {
            return (dTaxIncome);
        }
    }




Is This A Good Question/Topic? 0
  • +

Replies To: If else-if statement not executing last statement

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5633
  • View blog
  • Posts: 12,086
  • Joined: 02-June 10

Re: If else-if statement not executing last statement

Posted 16 September 2012 - 04:53 PM

First - that is just horrible. Sorry.
It looks like you're trying to do some sort of form validation.
If that's the case look at the ErrorProvider class. It pops up little red-circled exclamations next to controls that need attention. You just write the validation into a property.

Next: Controls like textboxes are not meant to be your variables to hold values. You should have properties backing these up and not looking at the actual textbox.text properties. Get used to separating your data from your GUI as early as you can and it won't make life hard on you later.

if (string.IsNullOrWhiteSpace(ClientName))
//not
if (txtName.Text.Length == 0)


As to the problem you wrote about...

The last else on line 56 is directly related to the last if on line 52. As soon as a radio button is checked that if will fail and the else should run. But that assumes you are once again hitting the button all this is within AFTER ticking one of the radio buttons.

Put a breakpoint on line 52 to confirm the values of those radio buttons.

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:

This post has been edited by tlhIn`toq: 16 September 2012 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Re: If else-if statement not executing last statement

Posted 16 September 2012 - 06:38 PM

Ok, now I really feel like an idiot but I am trying to learn. Like I said, I'm totally new at this and I'm following examples given to me by my instructor as far as the form validation. He wants to hit the calculate button with no data input and continue to get error messages in the "Result" field until all required data is input by the user. I will look at what you suggested about the ErrorProvider class, but I don't think we're that far along yet. I set the breakpoint and indeed if the interval is chosen by the user it steps right over the calculation code. I'm still very confused. Do I need to nest the if statements differently?
Was This Post Helpful? 0
  • +
  • -

#4 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 407
  • Joined: 04-January 11

Re: If else-if statement not executing last statement

Posted 16 September 2012 - 07:13 PM

Have you tried putting a breakpoint at GetTaxIncome and see if it is being triggered?

If it is, then the problem must either be inside this function itself or after the function is called instead of the if...else statements.

What I noticed is that it seems that you are not passing the parameters in correct order.

dAnswer = GetTaxIncome(dHealth, d401k, dEarnings, iInt);  



Your said your function is like this...

double GetTaxIncome(double dEarn, double dHlth, double d401, int iInterval)  

  {  

      double dTaxIncome;  

      dTaxIncome = dEarn * iInterval - ((dHlth + d401) * 12);  

      {  

          return (dTaxIncome);  

      }  

  } 



Shouldn't it be like this...?

dAnswer = GetTaxIncome(dEarnings ,dHealth, d401k,  iInt);  



where Earnings is passed first.

Another thing I noticed is that dAnswer is of the data type double so maybe this line won't work.

lblResults.Text = "Your taxable income is " + dAnswer;  



How about trying to convert it to string like this...

lblResults.Text = "Your taxable income is " + dAnswer.ToString();  



I hope this is of some help to you.

This post has been edited by November-06: 16 September 2012 - 07:15 PM

Was This Post Helpful? 1
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: If else-if statement not executing last statement

Posted 16 September 2012 - 07:14 PM

Look at lines 40 to 55. These lines are mutually exclusive and exhaustive in that if one is true, the others must be false and one of them is always true. Because of this, the else in line 56 can never be reached as all if checks before it must be false
Was This Post Helpful? 2
  • +
  • -

#6 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Re: If else-if statement not executing last statement

Posted 16 September 2012 - 07:36 PM

November-06, I didn't realize it mattered what order the parameters were called. Thank you. And I did at one point have dAnswer.ToString so that it ran ok. In my many, many edits I must have forgotten to put it back. I did put a breakpoint in and it's definitely in the "if" statement. Momerath, thank you. I will take another look at those lines. At this point, I've been working on this for hours trying so many different ways to make it work that I'm beyond frustrated. Maybe I need to walk away from it for a while and take another look tomorrow.
Was This Post Helpful? 0
  • +
  • -

#7 dhazard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 67
  • Joined: 06-September 06

Re: If else-if statement not executing last statement

Posted 17 September 2012 - 01:32 AM

View Postoldnewbie, on 16 September 2012 - 10:36 PM, said:

November-06, I didn't realize it mattered what order the parameters were called. Thank you. And I did at one point have dAnswer.ToString so that it ran ok. In my many, many edits I must have forgotten to put it back. I did put a breakpoint in and it's definitely in the "if" statement. Momerath, thank you. I will take another look at those lines. At this point, I've been working on this for hours trying so many different ways to make it work that I'm beyond frustrated. Maybe I need to walk away from it for a while and take another look tomorrow.


That always helps when I come into a tough problem. Additionally sometimes when you have a lot of logic going on writing it down on paper using Pseudo code helps me out a lot. Since I do have ADHD I tend to jump to far into the coding when I actually should have taken a step back and plan better.
Was This Post Helpful? 0
  • +
  • -

#8 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Re: If else-if statement not executing last statement

Posted 17 September 2012 - 11:12 AM

Just an update: After sleeping on it, I tried again today and finally got things working correctly. I ended up separating the "if" statements and it works great and performs the calculation as expected. Thanks to all!
Was This Post Helpful? 0
  • +
  • -

#9 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,537
  • Joined: 08-April 09

Re: If else-if statement not executing last statement

Posted 17 September 2012 - 02:24 PM

Wouldn't it be better if he switched his multiple if else with the switch statements?
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: If else-if statement not executing last statement

Posted 17 September 2012 - 07:41 PM

No, because they're not all testing against the same value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1