任何简单的方法来查看哪个IF条件是错误的?

fdk*_*fsf 0 c# asp.net .net-4.0

我发布这个问题是为了找到一种更简单的方法来实现结果.

我们有一个重要的IF声明,检查NULLstring.empty.像这样的东西:

if (string.IsNullOrEmpty(Empl.Name) || string.IsNullOrEmpty(Empl.last) ||
   string.IsNullOrEmpty(Empl.init) || string.IsNullOrEmpty(Empl.cat1) ||
   string.IsNullOrEmpty(Empl.history) || string.IsNullOrEmpty(Empl.cat2) ||
   string.IsNullOrEmpty(Empl.year) || string.IsNullOrEmpty(Empl.month) || 
   string.IsNullOrEmpty(Empl.retire) || string.IsNullOrEmpty(Empl.spouse) || 
   string.IsNullOrEmpty(Empl.children) || string.IsNullOrEmpty(Empl.bday) || 
   string.IsNullOrEmpty(Empl.hire)|| string.IsNullOrEmpty(Empl.death) || 
   string.IsNullOrEmpty(Empl.JobName) || string.IsNullOrEmpty(Empl.More) || 
   string.IsNullOrEmpty(Empl.AndMore))
{
    //Display message. Something like "Error: Name and Month is missing"
    return;
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我发现的解决这个问题的任何解决方案都非常耗时,并且需要编写更多代码.

是否有任何方法可以知道哪个值string.IsNullOrEmpty不必过多地更改此IF语句?更糟糕的是,我可以单独检查每个单独的声明,但我不希望这样做.

谢谢.

D S*_*ley 5

不,没有"魔法"函数会告诉你OR语句中的一系列表达式是真的.此外,由于您使用的是短路版本,因此该语句将在第一个真实条件之后返回true,因此甚至不会评估剩余的表达式.

但是,你可以这样做:

bool[] checks = {
   string.IsNullOrEmpty(Empl.Name) , string.IsNullOrEmpty(Empl.last) ,
   string.IsNullOrEmpty(Empl.init) , string.IsNullOrEmpty(Empl.cat1) ,
   string.IsNullOrEmpty(Empl.history) , string.IsNullOrEmpty(Empl.cat2) ,
   string.IsNullOrEmpty(Empl.year) , string.IsNullOrEmpty(Empl.month) , 
   string.IsNullOrEmpty(Empl.retire) , string.IsNullOrEmpty(Empl.spouse) , 
   string.IsNullOrEmpty(Empl.children) , string.IsNullOrEmpty(Empl.bday) , 
   string.IsNullOrEmpty(Empl.hire) , string.IsNullOrEmpty(Empl.death) , 
   string.IsNullOrEmpty(Empl.JobName) , string.IsNullOrEmpty(Empl.More) , 
   string.IsNullOrEmpty(Empl.AndMore)
};

if(checks.Any())
{
    //Display message. Something like "Error: Name and Month is missing"
    return;
}
Run Code Online (Sandbox Code Playgroud)

现在checks变量保存每个表达式的结果.