c# -在If语句中控制程序流

本文关键字:控制 程序 语句 If | 更新日期: 2023-09-27 18:19:02

我有以下c#代码:

if (FileUpload_Certificate.HasFile == false)
{
    Label_Error.Visible = true;
    Label_Error.Text = "Error Message: Please upload your digital certificate";
}
else
{
    if (val.isCertificate(FileUpload_Certificate) == false)
    {
        Label_Error.Visible = true;
        Label_Error.Text = "Error Message: The file uploaded is not a certificate!";
    }
    else
    {
        if(val.hasValidCA(FileUpload_Certificate) == false)
        {
            Label_Error.Visible = true;
            Label_Error.Text = "Error Message: The certificate uploaded does not come from a trusted certification authority!";
        }
        else
        {
            if (val.hasPublicKey(FileUpload_Certificate) == false)
            {
                Label_Error.Visible = true;
                Label_Error.Text = "Error Message: The certificate uploaded is not in the correct format!";
            }
            else
            {
                if (val.hasValidDate(FileUpload_Certificate) == false)
                {
                    Label_Error.Visible = true;
                    Label_Error.Text = "Error Message: The certificate uploaded has expired!";
                }
                else
                {
                    if (val.EmptyString(ip_address) == false)
                    {
                        if (val.isIP(ip_address) == false)
                        {
                            Label_Error.Visible = true;
                            Label_Error.Text = "Error Message: The IP address field is not in the correct format!";
                        }
                    }
                    if (val.EmptyString(port_number) == false)
                    {
                        if (val.IsNumeric(port_number) == false)
                        {
                            Label_Error.Visible = true;
                            Label_Error.Text = "Error Message: The port number field must be composed of digits only!";
                        }
                        else
                        {
                            int port = 0;
                            try
                            {
                                port = Convert.ToInt32(port_number);
                            }
                            catch (Exception)
                            {
                                Label_Error.Visible = true;
                                Label_Error.Text = "Error Message: There was a problem converting the port number to integer!";
                            }
                            if (port > 65535)
                            {
                                Label_Error.Visible = true;
                                Label_Error.Text = "Error Message: The port number field must be less than 65536!";
                            }
                        }
                    }
                }
            }
        }
    }
}

可以看到,我首先检查的是用户上传的证书是否有效。这些检查必须始终执行。

之后,我检查用户输入的ip地址和端口号是否有效。但是,不同之处在于,如果用户在ip地址和端口号字段中输入了一些文本,则应该执行这些检查。如果用户在两个字段中都没有输入任何内容,则应该跳过检查。

此外,如果其中任何一个字段中有文本,则会出现错误消息,建议用户在两个字段中都输入文本。

当前的代码是,如果用户在ip地址或端口号字段中输入文本,而不是同时输入文本,则不会显示错误消息。请问我该如何解决这个问题?

c# -在If语句中控制程序流

以下不是答案,而是一些一般性的建议,告诉你如何更好地回答自己的问题;

这是一种很难理解的编码风格。有太多嵌套的代码块,很难遵循逻辑。

写这类代码的"更好"的方法是自由使用return语句,像这样;

if (FileUpload_Certificate.HasFile == false)
{
    Label_Error.Visible = true;
    Label_Error.Text = "Error Message: Please upload your digital certificate";
    return;
}
if (val.isCertificate(FileUpload_Certificate) == false)
{
    Label_Error.Visible = true;
    Label_Error.Text = "Error Message: The file uploaded is not a certificate!";
    return;
}

当嵌套像这样简化后,解决实际问题可能会容易得多。

IT还将使隔离有问题的实际区域变得更容易。在本例中,这行之前的所有代码;

if (val.EmptyString(ip_address) == false)

是完全不相关的,只是掩盖了真正的问题。