开发者

Implicit conversion error in C# application [closed]

开发者 https://www.devze.com 2023-03-21 07:23 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about programming within the scope defined in the help center.

Closed 9 years ago.

Improve this question

I get this error when running my application:

Error 2 开发者_开发问答Cannot implicitly convert type 'object' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

The code:

public object BirthDate
{
    get { return date_ofbirth; }
    set { date_ofbirth = value; }
}


Looks like the variable date_ofbirth is defined as DateTime and the property wrapper BirthDate is marked as return/accept object.

Change:

public object BirthDate 
{ 
    get { return date_ofbirth; } 
    set { date_ofbirth = value; } 
}

to

public DateTime BirthDate 
{ 
    get { return date_ofbirth; } 
    set { date_ofbirth = value; } 
}


It's not clear from the code you've given what your doing with BirthDate but from the exception given it looks like you need it to be a DateTime object. E.g...

public DateTime BirthDate { get {} set {} }

If this isn't what you want let us know what you are trying to do.


you are either going to have to change the property BirthDate to be a System.DateTime or cast:

 DateTime myDate  = (DateTime)BirthDate;
0

精彩评论

暂无评论...
验证码 换一张
取 消