【发布时间】:2016-10-09 05:22:08
【问题描述】:
我发布这个问题是为了找到一种更简单的方法来获得结果。
我们有一个大的IF 语句,用于检查NULL 或string.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;
}
到目前为止,我发现的任何解决方案都非常耗时,并且需要编写更多代码。
有没有什么方法可以知道哪个值是string.IsNullOrEmpty 而不必过多地更改这个 IF 语句?更糟糕的是,我可以单独检查每个语句,但我不想这样做。
谢谢。
【问题讨论】:
-
我不会这样做。我会想办法将它们封装到 Employee 类中。通过契约、前提条件和合理的默认值来思考编程。