[英]Fix System.Datetime not all path return a value C#
I want to return boolean value from DateTime calculation but there error in line function EnableEdit(Datetime URD)
System.Datetime not all path return a value. 我想从DateTime计算返回布尔值,但行函数EnableEdit(Datetime URD)
System.Datetime中存在错误,并非所有路径都返回值。 Here my code 这是我的代码
public static int DayAfterReg(DateTime URD) {
int totalDay = (int)(URD - DateTime.Now).TotalDays;
return totalDay;
}
public static Boolean EnableEdit(DateTime URD)
{
if (UserClass.DayAfterReg(URD)<=3){
return true;
}
else if (UserClass.DayAfterReg(URD) >3)
{
return false;
}
}
How to solve it? 怎么解决呢?
There is a branch in your code which doesn't return any value (if <= 3 and >3 ), but this is not a possible case so you can rewrite your method like this: 您的代码中有一个分支不会返回任何值(如果<= 3和> 3),但是这是不可能的,因此您可以像这样重写您的方法:
public static Boolean EnableEdit(DateTime URD)
{
return UserClass.DayAfterReg(URD) <= 3;
}
This will simply return the result from the expression, which covers all possible cases. 这将简单地从表达式中返回结果,该结果涵盖所有可能的情况。
Put an else there: 在此放置另一个:
public static Boolean EnableEdit(DateTime URD)
{
if (UserClass.DayAfterReg(URD) <= 3){
return true;
}
/*else if (UserClass.DayAfterReg(URD) > 3)
{
return false;
}*/
//in your case, you don't need to put an "else if" condition here, it is obvious if first condition(<= 3) is false, then else condition will be " > 3"
else
return false;
}
There must be a condition which returns something. 必须有一个条件,该条件会返回某些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.