Reputation: 23
This may be the incorrect way of asking this but I need to figure out how to have the value that I am parsing in from a textbox to be 0 if it is left blank.
I have two textbox's (LaborCharge & PartsCharge)
the user does not need to enter in a value for the charges. Assuming that they left one of both of the textboxes blank, I want the value that I am parsing out of the textbox to = 0 so I can use it to calculate a total for labor and parts.
Thanks!
Upvotes: 2
Views: 2599
Reputation: 1
You can always set a default value of 0 to each TextBox you create. this way you don't have to deal with a null value, which sometimes causes some difficulties if you don't pay attention (late night programming...).
I find that users usually don't mind 0 values in empty boxes, and it is much easier later on to write:
if(TextBox.Text = "0")
// enter your code in here
Upvotes: 0
Reputation: 54532
compare the TextBox's Values with String.IsNullOrEmpty
or if you are running .Net 4.0 String.IsNullOrWhiteSpace
would be better suited for you.
if (String.IsNullOrEmpty(LaborCharge.Text))
//Set your variable to zero
else
//Process your code
Upvotes: 2
Reputation: 61339
Use something like the following:
int charge = 0;
int.TryParse(LaborCharge.Text, out charge);
...
Try parse will set charge to zero if the conversion fails due to the string being null, empty or not containing a number. (MSDN)
Upvotes: 10