nullable object must have a value

There is paradox in the exception description:
Nullable object must have a value (?!)

This is the problem:

I have a DateTimeExtended class,
that has

{
  DateTime? MyDataTime;
  int? otherdata;

}

and a constructor

DateTimeExtended(DateTimeExtended myNewDT)
{
   this.MyDateTime = myNewDT.MyDateTime.Value;
   this.otherdata = myNewDT.otherdata;
}

running this code

DateTimeExtended res = new DateTimeExtended(oldDTE);

throws an InvalidOperationException with the message:

Nullable object must have a value.

myNewDT.MyDateTime.Value – is valid and contain a regular DateTime object.

What is the meaning of this message and what am I doing wrong?

Note that oldDTE is not null. I’ve removed the Value from myNewDT.MyDateTime but the same exception is thrown due to a generated setter.

8 Answers
8

Leave a Comment