简体   繁体   English

修复System.Datetime并非所有路径都返回值C#

[英]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.

相关问题 无法将空值分配给C#中类型为System.DateTime的成员 - The null value cannot be assigned to a member with type System.DateTime in C# 为什么C#中没有System.DateTime的“日期”简写? - Why is there no "date" shorthand of System.DateTime in C#? 如何在C#codeDom中为System.DateTime使用CodeObjectCreateExpression - How to use CodeObjectCreateExpression for System.DateTime in C# codeDom 无法在C#中将类型字符串隐式转换为system.datetime - cannot implicitly convert type string to system.datetime in c# C#无法将类型&#39;string&#39;隐式转换为&#39;System.DateTime&#39; - C# Cannot implicitly convert type 'string' to 'System.DateTime' 无法将布尔类型隐式转换为System.DateTime(C#) - Cannot implicitly convert type bool to System.DateTime (C#) 无法将“字符串”转换为“System.DateTime” C# WPF - Cannot convert 'string' to 'System.DateTime' C# WPF 无法在 C# 中将类型“字符串”隐式转换为“System.DateTime” - Cannot implicitly convert type 'string' to 'System.DateTime' in C# 如何修复错误参数3:c#中无法从&#39;System.DateTime&#39;转换为&#39;Microsoft.Win32.Registry ValueOptions&#39; - how to fix error Argument 3: cannot convert from 'System.DateTime' to 'Microsoft.Win32.Registry ValueOptions' in c# C# asp.net 核心 webapi 抛出“JSON 值无法转换为 System.Nullable`1[System.DateTime] - C# asp.net core webapi throwing "The JSON value could not be converted to System.Nullable`1[System.DateTime]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM