-
Try/Catch Statements
Hello there! I am fairly new to C# and I'm having a problem coding a specific assignment. I was wondering if anyone could provide a few tips on what I'm doing wrong. I'm trying to have it catch 3 different things. That there is a decimal value, that there actually is something inserted, and that the number is within range of 0 and 1000. All of these need to display a different message box also. Down below is so far what I've done. I'm able to do the valid range but it's still calculating the numbers when I click calculate. Aslo when there is no value entered it brings up the "Please enter a valid subtotal" message box instead of the "Requires Subtotal" box. Thanks!
Code:
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
decimal subtotal = Decimal.Parse(txtSubtotal.Text);
if (subtotal < 0 || subtotal > 1000)
{
MessageBox.Show("Please enter a valid range");
txtSubtotal.Focus();
}
if (txtSubtotal.Text == "")
{
MessageBox.Show("Requires Subtotal");
return false;
}
decimal discountPercent = .25m;
decimal discountAmount = subtotal * discountPercent;
decimal invoiceTotal = subtotal - discountAmount;
discountAmount = Math.Round(discountAmount, 2);
invoiceTotal = Math.Round(invoiceTotal, 2);
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString();
txtTotal.Text = invoiceTotal.ToString();
txtSubtotal.Focus();
Convert.ToDecimal(txtSubtotal.Text);
}
catch (FormatException)
{
MessageBox.Show("Please enter a valid subtotal");
}
}
-
Re: Try/Catch Statements
It works just as you designed it.
This line
Code:
decimal subtotal = Decimal.Parse(txtSubtotal.Text);
Throws a FormatException if the value is unabled to be parsed. And the code in the catch statement i executed.
Run the code in the debuger and you see where the exception occurs.
-
Re: Try/Catch Statements
Let's think about the logic of your program, in comments:
Code:
//convert the value the user typed into a decimal
decimal subtotal = Decimal.Parse(txtSubtotal.Text);
//check it's within range
if (subtotal < 0 || subtotal > 1000)
{
MessageBox.Show("Please enter a valid range");
txtSubtotal.Focus();
}
//check if the user typed anything or not
if (txtSubtotal.Text == "")
{
MessageBox.Show("Requires Subtotal");
return false;
}
decimal discountPercent = .25m;
Can you see a problem?
Here's a clue, I've stripped the code, leaving just 2 relevant comments:
Code:
//first, take the value the user typed, and convert it to a decimal
//then, check if the user typed something
What strikes you about this logic? :)
-
When you start off in programing, write the commentfirst in plain english and embody the logic, then turn the comments into code. You end up with a program that works because you first wrote it in the language that you think in, and you have a beautifully commented code at the end
Good result!
-
Re: Try/Catch Statements
cool...
Please i know it's odd to ask this here how do i put signatures here for my site with a link like this : Jobs in Nigeria today