Groups | Blog | Home
all groups > dotnet windows forms designtime > april 2007 >

dotnet windows forms designtime : Serializer can't set a property to null


VisualHint
4/19/2007 11:28:33 AM
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
4/19/2007 12:10:47 PM
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


[quoted text, click to view]

ravi.s.desai NO[at]SPAM gmail.com
4/23/2007 10:18:19 AM
[quoted text, click to view]

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
4/24/2007 4:11:11 PM
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

On Apr 23, 1:18 pm, "ravi.s.de...@gmail.com" <ravi.s.de...@gmail.com>
[quoted text, click to view]

AddThis Social Bookmark Button