Strange SmartDate behaviour

Strange SmartDate behaviour

Old forum URL: forums.lhotka.net/forums/t/6864.aspx


Michael posted on Tuesday, April 28, 2009

DateTime? date = null;
var smartDate = new SmartDate(date, false);


You would expect smartDate.IsEmpty == true but it's not.

In the constructor the line:
_date = DateTime.MinValue;
should be
_date =  emptyIsMin ? DateTime.MinValue : DateTime.MaxValue;

and in the Date property:
_date = DateTime.MinValue;
should be
_date =  EmptyIsMin ? DateTime.MinValue : DateTime.MaxValue;


The same applies to public SmartDate(DateTime? value, EmptyValue emptyValue)

Or am I missing something?

Regards
Michael

JoeFallon1 replied on Wednesday, April 29, 2009

Have you read the book on this?
As I recall there is some "interesting" initialization behavior required to make it work correctly. Not sure if this is part of that or not.

Michael replied on Wednesday, April 29, 2009

Hi Joe

I have just read the relevant pages from the book (p. 490 in particular).
I can see the interesting initialisation due to the idiosyncrasies of structs. However, I still think if you create a SmartDate using a constructor that specifies either emptyIsMin or an EmptyValue, but without a date value, the SmartDate should return true for IsEmpty. Currently, this only works if you pass in emptyIsMin == true or EmptyValue.MinValue.

I discovered this through a unit test that was failing. I have a nullable date field in the database, being stored in the biz object in a SmartDate initialised with EmptyValue.MaxValue - infinitely far in the future, as Rocky says. In the Fetch method:

m_dateReturned = new SmartDate(data.DateReturned, m_dateReturned.EmptyIsMin);

If data.DateReturned is null, then m_dateReturned.Text == "01-01-0001" instead of empty string. Which means I'd have to check the value for null before constructing the SmartDate - but only where I want to treat empty dates as infinitely far in the future. I think this is inconsistent, and is pretty easily fixed. In fact, the method SetEmptyDate(EmptyValue emptyValue) already does what I described in my first post, it's just not called in SmartDate(DateTime? value, bool emptyIsMin) or SmartDate(DateTime? value, EmptyValue emptyValue) which looks like an oversight.

new SmartDate(EmptyValue.MaxValue)
should be the same as
new SmartDate(null,
EmptyValue.MaxValue)
but it's not.

Regards
Michael

ajj3085 replied on Thursday, April 30, 2009

I think I have to agree that it should work the same way in either constructor. The semantics seem off.

Copyright (c) Marimer LLC