views:

62

answers:

6

I've been working through this for a while and I'm missing the obvious. I'm trying to make sure that if a textbox value is left blank an error isn't thrown and instead a simple message is displayed in the textbox. However, what I've got and several other methods I've tried similar to this haven't worked. I can't figure out why this isn't working and what I need to do differently.

Basics:

This is a simple calculator that allows one to enter their waist, neck, and height measurements to use in a formula that calculates their estimated body fat percentage. The calculation works properly unless a field is left blank.

Thanks for any help!

My code:

        if (TBWaist.Text == null || TBNeck.Text == null || TBHeight.Text == null)
        {
            TBBodyFat.Text = "Value missing";
        }
        else
            if (TBWaist.Text != null & TBNeck.Text != null & TBHeight.Text != null)
            {
                double waist;
                double neck;
                double height;

                waist = Convert.ToDouble(TBWaist.Text);
                neck = Convert.ToDouble(TBNeck.Text);
                height = Convert.ToDouble(TBHeight.Text);

                TBBodyFat.Text = Convert.ToString(String.Format("{0:p2}", ((501.5 / (1.0324 - .19077 * (Math.Log10(waist - neck)) + .15456 * (Math.Log10(height))) - 450) / 100)));

Error Message

This is the error message I get if I leave the waist textbox blank. I get the same error if I leave any of the others blank as well.

Input string was not in a correct format on line 45.

waist = Convert.ToDouble(TBWaist.Text);
+1  A: 

How about using a RequiredFieldValidator?

Alternatively, you can check that the value is an empty string and/or null

if (
    String.IsNullOrEmpty(TBWaist.Text) ||
    String.IsNullOrEmpty(TBNeck.Text) ||
    String.IsNullOrEmpty(TBHeight.Text)
)
Marko
A: 

Try a compare like

if( String.IsNullOrEmpty( TBWaist.Text ) == true )
{
     // error case
}
else
{
     // do stuff
}
Viper
Why would we compare a `bool` to true?
Steven Sudit
@Steven: this is just our coding standard, because anybody can see directly to which value you compare. It is not really neccessary.
Viper
@Viper: You may well be bound by that standard, but on objective terms, it's not very good because it's redundant and arbitrary. The variable is already a bool. By that logic, you should be testing the result of the comparison with `true` by comparing it once again to `true`, ad infinitum. :-)
Steven Sudit
A: 

You should use String.IsNullOrEmpty() instead. Your input field values are not null, simply empty, hence the conversion fails.

protected void Button1_Click(object sender, EventArgs e)
{
    //Navy Men's Body Fat Formula:  %Fat=495/(1.0324-.19077(log(abdomen-neck))+.15456(log(height)))-450 
    //string outputString = String.Format("At loop position {0}.\n", i);


    if (String.IsNullOrEmpty(TBWaist.Text) || String.IsNullOrEmpty(TBNeck.Text) || String.IsNullOrEmpty(TBHeight.Text))
    {
        TBBodyFat.Text = "Value missing";
    }
    else
    {
        double waist;
        double neck;
        double height;

        waist = Convert.ToDouble(TBWaist.Text);
        neck = Convert.ToDouble(TBNeck.Text);
        height = Convert.ToDouble(TBHeight.Text);

        TBBodyFat.Text = Convert.ToString(String.Format("{0:p2}", ((501.5 / (1.0324 - .19077 * (Math.Log10(waist - neck)) + .15456 * (Math.Log10(height))) - 450) / 100)));
    }

}
Tchami
+2  A: 

You should test for more than just null values you have to test for empty as well

use String.IsNullOrEmpty(TBWaist.Text);

Sruly
+4  A: 

I would recommend using the TryParse methods. In this way, a blank, an empty string or something that is not convertible to a string are all treated the same. Second, I would recommend adding RequiredFieldValidators and/or RegularExpressionValidators to each of the text boxes to ensure that the user enters a value and that value is numeric. In this way, the check in your event procedure acts as a last ditch check instead of requiring a PostBack for validation

protected void Button1_Click(object sender, EventArgs e)
{
    //Navy Men's Body Fat Formula:  %Fat=495/(1.0324-.19077(log(abdomen-neck))+.15456(log(height)))-450 
    //string outputString = String.Format("At loop position {0}.\n", i);

    double waist;
    double neck;
    double height;

    if ( !double.TryParse( TBWaist.Text, out waist )
        || !double.TryParse( TBNeck.Text, out neck )
        || !double.TryParse( TBHeight.Text, out height ) )
    {
        ErrorMessageLabel.Text = "Please ensure that each value entered is numeric.";
        return;
    }   

    var bodyFat = (501.5 
        / (1.0324 - .19077 * (Math.Log10(waist - neck)) 
            + .15456 * (Math.Log10(height))
            ) - 450 ) / 100;

    TBBodyFat.Text = bodyFat.ToString("P2");
}
Thomas
This is the most comprehensive answer. The only thing I might suggest is that they call `Trim` on the `Text` before `TryParse`.
Steven Sudit
@Steven Sudit - Trim is not necessary with TryParse. `"12345.6789"` will be parsed equally well as `"12345.6789 "` as well as `" 12345.6789 " `.
Thomas
You are correct, of course.
Steven Sudit
A: 

Many thanks Thomas! I appreciate the responses.

Tom