C# IF语句的自定义验证问题

C# IF语句的自定义验证问题,c#,C#,代码如下: if (roomGender == "M") { if (gender == "F") { row.Cells[5].BackColor = Color.FromName("#FF0000"); args.IsValid = false; vldGende

代码如下:

            if (roomGender == "M")
            {
                if (gender == "F")
                {
                    row.Cells[5].BackColor = Color.FromName("#FF0000");
                    args.IsValid = false;
                    vldGender.ErrorMessage = building +" " + room + ": You cannot place a female in this space";
                }
                else
                {
                    vldGender.ErrorMessage = "";
                }
            }
            //end male gender check
            //Female gender check
            if (roomGender == "F")
            {
                if (gender == "M")
                {
                    row.Cells[5].BackColor = Color.FromName("#FF0000");
                    args.IsValid = false;
                    vldGender.ErrorMessage = building +" " + room + ": You cannot place a male in this space";
                }
                else
                {
                    vldGender.ErrorMessage = "";
                }
            }
            //end female gender check
            //Validate Names
            string last = ((TextBox)row.FindControl("txtLast")).Text;
            string first = ((TextBox)row.FindControl("txtFirst")).Text;

            if (last == "" && first != "")
            {
                row.Cells[3].BackColor = Color.FromName("#FF0000");
                args.IsValid = false;
                vldLast.ErrorMessage = building +" " + room + ": The last name cannot be blank";
            }
            else
            {
                vldLast.ErrorMessage = "";
            }
            if (last != "" && first == "")
            {
                row.Cells[4].BackColor = Color.FromName("#FF0000");
                args.IsValid = false;
                vldFirst.ErrorMessage = building +" " + room + ": The first name cannot be blank";
            }
            else
            {
                vldFirst.ErrorMessage = "";
            }

            if (last != "" && first != "" && gender == "")
            {
                row.Cells[5].BackColor = Color.FromName("#FF0000");
                args.IsValid = false;
                vldGender2.ErrorMessage = building +" " + room + ": A gender must be selected";
            }
            else
            {
                vldGender2.ErrorMessage = "";
            }
            if (!(regLast.IsValid))
            {
                row.Cells[3].BackColor = Color.FromName("#FF0000");
                regLast.ErrorMessage = building +" " + room + ": The last name is incorrect, please check the name";
            }
            if (!(regFirst.IsValid))
            {
                row.Cells[4].BackColor = Color.FromName("#FF0000");
                regFirst.ErrorMessage = building +" " + room + ": The first name is incorrect, please check the name";
            }
        }
    }
}
我的问题 由于这是在其中一个if语句无法验证时使用if语句,因此if语句将在该行停止。因此,该行上的其余字段未被验证

我有字段名、姓和性别

如果我忘了加上名字和姓氏,但不加性别

此验证只会显示我缺少名字,在名字被修复之前不会检查姓氏


他们是否有办法解决这个问题,以便同时检查两个字段?

我不确定在验证失败的地方返回到哪里,但我已经重新组织了您的代码,以便更高效、更易于维护。Vlgender和Vlgender2有什么原因吗?regFirst和regLast做什么

此外,应避免使用==和!=比较字符串值时。检查“”或null时,应使用string.Equals或string.IsNullOrEmpty。我还可以为您的一些错误消息推荐String.Format

if (roomGender.Equals("M"))
{
    if (gender.Equals("F"))
    {
        row.Cells[5].BackColor = Color.FromName("#FF0000");
        args.IsValid = false;
        vldGender.ErrorMessage = building + " " + room + ": You cannot place a female in this space";
    }
    else
    {
        vldGender.ErrorMessage = "";
        vldGender2.ErrorMessage = "";
    }

}
//end male gender check
//Female gender check
else if (roomGender.Equals("F"))
{
    if (gender.Equals("M"))
    {
        row.Cells[5].BackColor = Color.FromName("#FF0000");
        args.IsValid = false;
        vldGender.ErrorMessage = building + " " + room + ": You cannot place a male in this space";
    }
    else
    {
        vldGender.ErrorMessage = "";
        vldGender2.ErrorMessage = "";
    }
}
//end female gender check
// No gender selected
else
{
    row.Cells[5].BackColor = Color.FromName("#FF0000");
    args.IsValid = false;
    vldGender2.ErrorMessage = building + " " + room + ": A gender must be selected";
}

//Validate Names
string last = ((TextBox)row.FindControl("txtLast")).Text;
string first = ((TextBox)row.FindControl("txtFirst")).Text;

if (string.IsNullOrEmpty(last))
{
    row.Cells[3].BackColor = Color.FromName("#FF0000");
    args.IsValid = false;
    vldLast.ErrorMessage = building + " " + room + ": The last name cannot be blank";
}
else            
    vldLast.ErrorMessage = "";

if (string.IsNullOrEmpty(first))
{
    row.Cells[4].BackColor = Color.FromName("#FF0000");
    args.IsValid = false;
    vldFirst.ErrorMessage = building + " " + room + ": The first name cannot be blank";
}
else
    vldFirst.ErrorMessage = "";

if (!(regLast.IsValid))
{
    row.Cells[3].BackColor = Color.FromName("#FF0000");
    regLast.ErrorMessage = building + " " + room + ": The last name is incorrect, please check the name";
}

if (!(regFirst.IsValid))
{
    row.Cells[4].BackColor = Color.FromName("#FF0000");
    regFirst.ErrorMessage = building + " " + room + ": The first name is incorrect, please check the name";
}

我不确定在验证失败的地方它会返回到哪里,但我已经重新组织了您的代码,使其更高效、更易于维护。Vlgender和Vlgender2有什么原因吗?regFirst和regLast做什么

此外,应避免使用==和!=比较字符串值时。检查“”或null时,应使用string.Equals或string.IsNullOrEmpty。我还可以为您的一些错误消息推荐String.Format

if (roomGender.Equals("M"))
{
    if (gender.Equals("F"))
    {
        row.Cells[5].BackColor = Color.FromName("#FF0000");
        args.IsValid = false;
        vldGender.ErrorMessage = building + " " + room + ": You cannot place a female in this space";
    }
    else
    {
        vldGender.ErrorMessage = "";
        vldGender2.ErrorMessage = "";
    }

}
//end male gender check
//Female gender check
else if (roomGender.Equals("F"))
{
    if (gender.Equals("M"))
    {
        row.Cells[5].BackColor = Color.FromName("#FF0000");
        args.IsValid = false;
        vldGender.ErrorMessage = building + " " + room + ": You cannot place a male in this space";
    }
    else
    {
        vldGender.ErrorMessage = "";
        vldGender2.ErrorMessage = "";
    }
}
//end female gender check
// No gender selected
else
{
    row.Cells[5].BackColor = Color.FromName("#FF0000");
    args.IsValid = false;
    vldGender2.ErrorMessage = building + " " + room + ": A gender must be selected";
}

//Validate Names
string last = ((TextBox)row.FindControl("txtLast")).Text;
string first = ((TextBox)row.FindControl("txtFirst")).Text;

if (string.IsNullOrEmpty(last))
{
    row.Cells[3].BackColor = Color.FromName("#FF0000");
    args.IsValid = false;
    vldLast.ErrorMessage = building + " " + room + ": The last name cannot be blank";
}
else            
    vldLast.ErrorMessage = "";

if (string.IsNullOrEmpty(first))
{
    row.Cells[4].BackColor = Color.FromName("#FF0000");
    args.IsValid = false;
    vldFirst.ErrorMessage = building + " " + room + ": The first name cannot be blank";
}
else
    vldFirst.ErrorMessage = "";

if (!(regLast.IsValid))
{
    row.Cells[3].BackColor = Color.FromName("#FF0000");
    regLast.ErrorMessage = building + " " + room + ": The last name is incorrect, please check the name";
}

if (!(regFirst.IsValid))
{
    row.Cells[4].BackColor = Color.FromName("#FF0000");
    regFirst.ErrorMessage = building + " " + room + ": The first name is incorrect, please check the name";
}

这是一个典型的情况,代码对于一个函数来说变得太复杂了

如果您将每个字段作为一个独立的步骤进行验证,那么它将变得更具可读性和可维护性

void ValidateRoomGender() 
{
     if(string.IsNullOrEmpty(roomGender))
     {
         vldRoomGender = "Please enter a room gender";
     }
     else if ((roomGender != 'M') && (roomGender != 'F'))
     {
         vldRoomGender = "Invalid Value";
     }
     else
     {
         vldRoomGender = string.Empty;
     }
}

void ValidateGender() 
{
     if( ((vldGender == 'F') && (vldRoomGender == 'M')) || ((vldGender == 'M') && (vldRoomGender == 'F'))
     {
          vldGender = "The gender must match the room"
     }
     else if (string.IsNullOrEmpty(vldGender))
     {
        // etc
     }
}


void Validate()
{
      ValidateRoomGender();
      ValidateGender();
      ValidateFirstName();
      ValidateSurname();
}

这是一个典型的情况,代码对于一个函数来说变得太复杂了

如果您将每个字段作为一个独立的步骤进行验证,那么它将变得更具可读性和可维护性

void ValidateRoomGender() 
{
     if(string.IsNullOrEmpty(roomGender))
     {
         vldRoomGender = "Please enter a room gender";
     }
     else if ((roomGender != 'M') && (roomGender != 'F'))
     {
         vldRoomGender = "Invalid Value";
     }
     else
     {
         vldRoomGender = string.Empty;
     }
}

void ValidateGender() 
{
     if( ((vldGender == 'F') && (vldRoomGender == 'M')) || ((vldGender == 'M') && (vldRoomGender == 'F'))
     {
          vldGender = "The gender must match the room"
     }
     else if (string.IsNullOrEmpty(vldGender))
     {
        // etc
     }
}


void Validate()
{
      ValidateRoomGender();
      ValidateGender();
      ValidateFirstName();
      ValidateSurname();
}

如果确定变量不可能为null,则只应使用
Equals()
实例方法。使用==和!=字符串的运算符很好,最终将为您执行静态
String.Equals()
调用。如果要显式指定要执行的相等检查的类型,请使用
Equals()
重载。如果确定变量不可能为null,则应仅使用
Equals()
实例方法。使用==和!=字符串的运算符很好,最终将为您执行静态
String.Equals()
调用。如果要显式指定要执行的相等性检查的类型,请使用
Equals()
的重载。