Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / April 2007

Tip: Looking for answers? Try searching our database.

Serializer can't set a property to null

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
VisualHint - 19 Apr 2007 19:28 GMT
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 - 19 Apr 2007 20:10 GMT
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

> Hi,
>
[quoted text clipped - 18 lines]
>
> Nicolas
ravi.s.desai@gmail.com - 23 Apr 2007 18:18 GMT
> I should add that the first time it is serialized, if Value is null it
> is serialized correctly (myControl.Value = null).
[quoted text clipped - 26 lines]
>
> > 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 - 25 Apr 2007 00:11 GMT
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>
wrote:

> > I should add that the first time it is serialized, if Value is null it
> > is serialized correctly (myControl.Value = null).
[quoted text clipped - 39 lines]
> the serialization yourself.   That way you know exactly what it is
> doing.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.