Nullable DateTime


Found my second thing in .Net that really sucks. In .Net DateTime is a reference type and is therefore not nullable (Unlike the Java.Sql.Date class in java). I am talking to a SQL Server database with a nullable DateTime field and I want to accurately pass this around my application then out of a web service (that is supposed to be interoperable between .Net and Java clients) into a .Net based UI.

Other things that suck about DateTime is that if you create a DateTime and do not initialise it then the object is set to DateTime.MIN_VALUE which happens to be a date lower than SQL Server 2000 can support. I get the feeling that Dates and interoperability really don't mix...

All in all it is a bit smelly. Apparently fixed in .Net 2 - but until then I will have to employ some horrible work-around hack.


So, the hack I went with in the end was to wrap my problem DateTime in an element called "NullableDateTime" which just contained a single DateTime (xs:date) element. When I ran XSDObjectGen over it gives me a class which was nearly what I needed. Apparently this is the most interoperable way of doing it so that Java clients can also access the web service.


Martin, I am facing a similar problem. I would appreciate if you could email me the class or provide some details.
I have never used XSDObjectGen tool.
I appreciate your help.



Not sure if you're still reading these comments but NHibernate-contrib provides classes that wrap reference types (booleans, dates, strings, etc) in Nullable types to address that very issue. I think I've seen it as a pattern either in the GoF book or on the wiki.

Congrats on the new position at Sourcegear/Teamprise!


Sorry I meant value types not reference types.

Cheers Michael, I've been hearing a lot of buzz about NHibernate but haven't checked it out yet - I think I will do that right now!


Creative Commons License
This blog is licensed under a Creative Commons License.