Checking field values ​​for null

I have been working on this for a while, and I am missing the obvious. I am trying to make sure that if the value of the text field is left empty, no error will be selected and instead a simple message will be shown in the text field. However, what I have, and a few other methods that I tried, it seems, did not work. I cannot understand why this does not work and what I need to do differently.

The basics:

This is a simple calculator that allows you to enter measurements of your waist, neck and height for use in a formula that calculates their estimated percentage of body fat. The calculation works correctly if the field remains empty.

Thanks for any help!

My code is:

        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

, , . , .

45.

waist = Convert.ToDouble(TBWaist.Text);
+3
5

TryParse. , , -, , . -, RequiredFieldValidators / RegularExpressionValidators , , , . , , PostBack

protected void Button1_Click(object sender, EventArgs e)
{
    //Navy Men 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");
}
+5

, , ,

String.IsNullOrEmpty(TBWaist.Text);

+2

How about using RequiredFieldValidator ?

Alternatively, you can verify that this value is an empty string and / or null

if (
    String.IsNullOrEmpty(TBWaist.Text) ||
    String.IsNullOrEmpty(TBNeck.Text) ||
    String.IsNullOrEmpty(TBHeight.Text)
)
+1
source

Try comparing for example

if( String.IsNullOrEmpty( TBWaist.Text ) == true )
{
     // error case
}
else
{
     // do stuff
}
0
source

Instead, you should use String.IsNullOrEmpty (). The values ​​of the input field are not null, just empty, so the conversion is not performed.

protected void Button1_Click(object sender, EventArgs e)
{
    //Navy Men 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)));
    }

}
0
source

Source: https://habr.com/ru/post/1758649/


All Articles