【发布时间】:2013-09-02 13:05:20
【问题描述】:
我的成员类中有一个 DOB 属性定义为可为空的 DateTime?:
public DateTime? DOB
{
get
{
var o = base.GetPropertyValue("memberDOB");
if (o == DBNull.Value)
{
return null;
}
return (DateTime?)o;
}
set
{
base.SetPropertyValue("memberDOB", value);
}
}
当值为空并且我试图检查它是否可以为空时 - 它只是一直说强制转换无效:
if((DateTime)_currentProfile.DOB == null)
txtDOB.Text = _currentProfile.DOB.ToString();
我试过了
TryParse(_currentProfile.DOB.ToString(), out dob)
_currentProfile.DOB == null
_currentProfile.DOB.ToString()
(DateTime)_currentProfile.DOB
它们都不起作用 - 它总是说强制转换无效。
不太明白为什么。
有什么想法吗?谢谢
【问题讨论】:
标签: datetime casting null nullable type-conversion