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);
}
}
9 Replies - 932 Views - Last Post: 17 September 2012 - 07:41 PM
#1
If else-if statement not executing last statement
Posted 16 September 2012 - 04:38 PM
Replies To: If else-if statement not executing last statement
#2
Re: If else-if statement not executing last statement
Posted 16 September 2012 - 04:53 PM
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:
- Debugging video 1: Breakpoints and Local variables
- Debugging video 2: Advanced breakpoints
- Debugging tutorial
- Debugging tips
- Debugging in detail
- Great debugging tips
- It still doesn't work, article
- Debugging tools survival guide
- Video: Tips & Tricks to use Visual Studio to the fullest
This post has been edited by tlhIn`toq: 16 September 2012 - 04:54 PM
#3
Re: If else-if statement not executing last statement
Posted 16 September 2012 - 06:38 PM
#4
Re: If else-if statement not executing last statement
Posted 16 September 2012 - 07:13 PM
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
#5
Re: If else-if statement not executing last statement
Posted 16 September 2012 - 07:14 PM
#6
Re: If else-if statement not executing last statement
Posted 16 September 2012 - 07:36 PM
#7
Re: If else-if statement not executing last statement
Posted 17 September 2012 - 01:32 AM
oldnewbie, on 16 September 2012 - 10:36 PM, said:
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.
#8
Re: If else-if statement not executing last statement
Posted 17 September 2012 - 11:12 AM
#9
Re: If else-if statement not executing last statement
Posted 17 September 2012 - 02:24 PM
#10
Re: If else-if statement not executing last statement
Posted 17 September 2012 - 07:41 PM
|
|

New Topic/Question
Reply



MultiQuote






|