DateTime tempDate = calculatesomedatetime();
someDateTimeControl.Value = null; //no issue
someDateTimeControl.Value = (tempDate > DateTime.MinValue)? tempDate : null;
Type of conditional expression cannot be determined because there is no implicit conversion between System.DateTime and null
Line 3 throwing me such error which I don't understand as the comparison is (tempDate > DateTime.MinValue)
and null
is just value assignment. Why would compiler interpret this as error?
However if I write as below, it has no problem
if(tempDate > DateTime.MinValue)
{
someDateTimeControl.Value = tempDate;
}else
{
someDateTimeControl.Value = null;
}
DateTime
is astruct
. It cannot be assigned tonull
. You need to either declare it asnullable
(like:DateTime? tempDate = ...
) or use some other default value, likeDateTime.MinValue
. – EdvardDateTime
. You still need to either definetempDate
as nullable, or cast it to nullable in the ternary expression. – Edvard