简体   繁体   English

错误返回时无法将类型'string'隐式转换为'System.DateTime'

[英]Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

I trying to add a propriety into a class but I'm getting the error: Error Cannot implicitly convert type 'string' to 'System.DateTime' on the else return. 我试图在类中添加适当性,但出现错误:错误无法在else返回上将类型'string'隐式转换为'System.DateTime'。

public DateTime BookDate
        {
            get
            {
                if (this.PXP.BookDate.HasValue)
                {
                    return this.PXP.BookDate.Value;
                }
                else return "";
            }
        }

I am not sure what to return here. 我不确定该返回什么。 I have to return something or else I get the error: not all code paths return a value. 我必须返回一些东西,否则会得到错误:并非所有代码路径都返回一个值。 BookDate is being called from another class: BookDate从另一个类被调用:

 public JobPXP PXP { get; set; }

I know this question is very simple.. and the answer is I was just missing the ? 我知道这个问题很简单..答案是我只是想念? after DataTime. 在DataTime之后。 Thank you everyone for their answers 谢谢大家的回答

public DateTime BookDate
    {
        get
        {
            if (this.PXP.BookDate.HasValue)
            {
                return this.PXP.BookDate.Value;
            }
            else return "";
        }
    }

From your code it looks like you want to pass empty date when this.PXP.BookDate.HasValue is false. 从您的代码看来,当this.PXP.BookDate.HasValue为false时,您想传递空日期。

The error is for - else return ""; 错误是-否则返回“”; statement. 声明。 You are trying to pass string in return statement. 您正在尝试在return语句中传递字符串。 Here is my suggestion (I am assuming this.PXP.BookDate is nullable DateTime as it looks from your code.) 这是我的建议(根据您的代码,我假设this.PXP.BookDate是可空的DateTime。)

public DateTime? BookDate
    {
        get
        {                
            return this.PXP.BookDate;                
        }
    }

I am not sure what to return here 我不确定该返回什么

If you are not sure what to return, probably you want to return null . 如果不确定返回什么,则可能要返回null However, the problem is that BookDate property is DateTime , which does not allow null s. 但是,问题在于BookDate属性是DateTime ,不允许使用null However, Nullable<DateTime> , or simply DateTime? 但是, Nullable<DateTime>还是仅仅是DateTime? does allow null s. 确实允许null Since this.PXP.BookDate is nullable, you can return it directly, like this: 由于this.PXP.BookDate可为空,因此可以直接将其返回,如下所示:

public DateTime? BookDate {
    get {
        return this.PXP.BookDate;
    }
}

Another solution is to make a method that would get an optional BookDate if it exists, similar to TryGetValue method of IDictionary<K,V> : 另一个解决方案是创建一个方法,该方法将获得可选的BookDate如果存在),类似于IDictionary<K,V> TryGetValue方法:

public bool TryGetBookDate(out DateTime res) {
    res = this.PXP.BookDate ?? default(DateTime);
    return this.PXP.BookDate.HasValue;
}

DateTime can not be null, so you can't return null . DateTime不能为null,因此您不能返回null A string is also an invalid option. 字符串也是无效的选项。 You've basically got two options here: 您基本上在这里有两个选择:

Either return a specific value of DateTime , such as DateTime.MinValue (which is 01.01.0001 ), or use a Nullable<DateTime> instead (equivalent to DateTime? ). 返回特定的DateTime值,例如DateTime.MinValue (它是01.01.0001 ),或者使用Nullable<DateTime>代替(等效于DateTime? )。

This last approach will let you return a Null values instead of an actual date, but will change the signature of the method, which in turn, will require handling when calling the method. 最后一种方法将使您返回Null值而不是实际日期,但是将更改方法的签名,从而在调用方法时需要进行处理。

You'll still have to handle special cases outside the method if you use DateTime.MinValue instead though, so I'd say it's up to you to choose what suits your situation best. 但是,如果您改用DateTime.MinValue ,则仍然需要在方法之外处理特殊情况,因此我想请您选择最适合您的情况。

Another Way: 其他方式:

public DateTime BookDate
        {
            get
            {
               DateTime dt = DateTime.MinValue;//you default datetime here
               DateTime.TryParse(Convert.ToString(this.PXP.BookDate.Value),out dt)
               return dt;  

            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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