Discussion:
Serializer can't set a property to null
(too old to reply)
VisualHint
2007-04-19 18:28:33 UTC
Permalink
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
VisualHint
2007-04-19 19:10:47 UTC
Permalink
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
Post by VisualHint
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
myControl.Value = null;
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
r***@gmail.com
2007-04-23 17:18:19 UTC
Permalink
Post by VisualHint
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
Post by VisualHint
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
myControl.Value = null;
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.
VisualHint
2007-04-24 23:11:11 UTC
Permalink
Thank you. No I don't use a nullable type.
Anyway, I was able to solve the issue by checking the passed date to
see if Ticks==0 and in that case I force the value to null. This works
well.

Nicolas
Post by r***@gmail.com
Post by VisualHint
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
Post by VisualHint
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
myControl.Value = null;
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.
Loading...