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

时间:2016-01-06 00:50:20

标签: c#

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

这是我到目前为止所得到的

// 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;
    }
}

我的问题是当我将值输入长度,宽度和深度时。它只是按顺序进行。我的意思是,如果我不输入宽度并将其留空并放入长度和深度,则会给我一个未处理的预期。

这是我的方法

/** 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;
    }
}

2 个答案:

答案 0 :(得分:2)

如果您为空字符串提供Parse方法,则会抛出异常。您应该捕获该异常,或使用TryParse

答案 1 :(得分:1)

你搞砸了你代码中的每一件事。首先有一个方法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;
}