简体   繁体   中英

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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM