IF输入验证的布尔语句顺序

本文关键字:语句 顺序 布尔 输入 验证 IF | 更新日期: 2023-09-27 18:29:16

我必须验证用户的一些输入,并发送错误消息

这就是我目前得到的

// probes the methods to check for validity.
private void btnCalculate_Click(object sender, EventArgs e)
{
    if (!(ValidWidth(float.Parse(txtWidth.Text))))
    {
        return;
    }
    if (!(ValidLength(float.Parse(txtLength.Text))))
    {
        return;
    }
    if (!(ValidDepth(float.Parse(txtAvgDepth.Text))))
    {
        return;
    }
}

我的问题是在"长度"、"宽度"answers"深度"中输入值时。它只是按顺序做的。。我的意思是,如果我不输入宽度,将其留空,并输入长度和深度,这会给我一个无法处理的期望。

这是我的方法

/** Created a boolean method to test if the written width is valid OR not valid **/
private bool ValidWidth(float Width1) {
   float Width = float.Parse(txtWidth.Text);
    {
        if (Width >= 2 & Width <= 20)
        {
            return true;
        }
        else
        {
            string Title = "Data Invalid";
            string Msg = "Width Measurement is invalid 'n Place enter a value between 2 and 20";
            DialogResult Response;
            Response = MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return false;
        }
    }
}
/** Created a boolean method to test if the written legnth is valid OR not valid **/
private bool ValidLength(float Length1)
{
    float Length = float.Parse(txtLength.Text);
    {
        if (Length >= 5 & Length <= 50)
        {
            return true;
        }
        else
        {
            string Title = "Data Invalid";
            string Msg = "Legnth Measurement is invalid 'n Place enter a value between 5 and 50";
            DialogResult Response;
            Response = MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return false;
        }
    }
}
/** Created a boolean method to test if the written legnth is valid OR not valid **/
private bool ValidDepth(float Depth1)
{
    float Depth = float.Parse(txtAvgDepth.Text);
    if (Depth >= 2 & Depth <= 4)
    {
        return true;
    }
    else
    {
        string Title = "Data Invalid";
        string Msg = "Average Depth Measurement is invalid 'n Place enter a value between 2 and 4";
        DialogResult Response;
        Response = MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return false;
    }
}

IF输入验证的布尔语句顺序

如果给Parse方法提供空字符串,它将抛出异常。您应该捕获该异常,或者使用TryParse

您把代码中的每一件事都搞砸了。首先是一个方法float.TryParse,它试图将字符串转换为浮点数。但如果转换失败,它不会抛出异常。相反,它给出了一个布尔值,告诉解析成功与否。

我觉得这样更好。

private void btnCalculate_Click(object sender, EventArgs e)
{
    if(!ValidateWidth(txtWidth.Text) || 
       !ValidateLength(txtLength.Text) ||
       !ValidateDepth(txtAvgDepth.Text)) // if any of these failed
    {
        MessageBox.Show(Msg, Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
}

我为您编写ValidateWidth作为示例。

private string Title = "Data Invalid";
private string Msg;
private bool ValidateWidth(string input)
{
    float width;
    if(float.TryParse(input, out width))
    {
        if (Width >= 2 && Width <= 20)
        {
            return true;
        }
    }
    Msg = "Width Measurement is invalid 'n Place enter a value between 2 and 20";
    return false;
}