[quoted text, click to view] On Apr 19, 3:10 pm, VisualHint <cadil...@gmail.com> wrote:
> I should add that the first time it is serialized, if Value is null it
> is serialized correctly (myControl.Value = null).
> But as soon as I edit the property and set it to a date, then erase
> the property in the grid with an empty string, it is serialized as
> System.DateTime(((long)(0))); instead of null.
>
> Nicolas
>
> On Apr 19, 2:28 pm, VisualHint <cadil...@gmail.com> wrote:
>
> > Hi,
>
> > Let's say that in my own custom control (derived from Control), I have
> > a property Value of type object. My goal is to store in this property
> > a DateTime or null. In order to store and edit a DateTime, I assign to
> > it the DateTimeConverter and DateTimeEditor attributes. All is fine at
> > runtime. However at design time, when I set an empty string in the
> > PropertyGrid (which is accepted by the grid), the serializer refuses
> > to write something like:
>
> > myControl.Value = null;
>
> > Instead it writes:
>
> > myControl.Value = new System.DateTime(((long)(0)));
>
> > Am I obliged to write my own serializer or is there something else I
> > can do ?
>
> > Thank you for your help
>
> > Nicolas
Your problem probably stems from the fact that DateTime is a
"structure" not a "class" and cannot be assigned the value null. Are
you using Nullable<DateTime> (also written DateTime?). Whenever we
use DateTime and need to represent "null", we either use
Nullable<DateTime> or simply assign it to DateTime.MinValue, and in
our data mapping code translate MinValue to DbNull and visa-versa.
I've noticed that there are several cases where the default
serialization can be a little finicky. In those cases, it is probably
a better idea to have your class extend ISerializable and implement
the serialization yourself. That way you know exactly what it is
doing.