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!
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.
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!
Bookmarks