LoadProperty and Milliseconds

LoadProperty and Milliseconds

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


TJones posted on Friday, October 10, 2008

When using LoadProperty<SmartDate, DateTime?>(myDateProperty, data.myDate) the milliseconds are ignored (set to zero).

I've traced this problem to the CoerceValue() method where the line 169, "tmp.Text = value.ToString();" ignores milisseconds.

Is this a known issue?

JoeFallon1 replied on Friday, October 10, 2008

I think it depends on your default format string.

As I recall the default is short date.

If you want something different you can set it for each SmartDate or you can set it once for all SmartDates when the app starts up.

Joe

 

TJones replied on Friday, October 10, 2008

I'm sorry, I don't see where to set a format string that would help.

When I call LoadProperty<SmartDate, DateTime?>(myDateProperty, data.myDate), Csla.CoerceValue() is eventually called and line 169, "tmp.Text = value.ToString();", uses the ToString() of the original DateTime? value (which is "10/10/2008 2:30:14 PM" without milliseconds) to create the tmp SmartDate that is then returned as the property's value.

Is there something that needs to be done to the original value (the DateTime?) so ToString() includes milliseconds?

vdhant replied on Sunday, October 12, 2008

Just a thought and I am not sure if this would work, but what happens if you create a new SmartDate class which inherits from the CSLA SmartDate. That what you can override the ToString implementation.

I am not sure if this would work, but it might - I would need to re-look at the CoerceValue code again, its been a while.

Hope that helps
Anthony

TJones replied on Monday, October 13, 2008

The offending ToString() is actually called on the loaded value (the DateTime) not the target, SmartDate value.

A solution, that appears to have low impact on the existing code, is to change line 169 of Csla.CoerceValue() to the following:

        if (value.GetType().Equals(typeof(DateTime)))
          tmp.Date = (DateTime)value;
        else
          tmp.Text = value.ToString();

This allows direct DateTime and DateTime? conversions to SmartDate, maintaining precision while null values, which are set to string.Empty in an earlier statement, are handled in the "else" (the original line if code).

RockfordLhotka replied on Tuesday, October 14, 2008

Thanks for tracking down the details of this issue - I'll add it to the work list.

http://www.lhotka.net/cslabugs/edit_bug.aspx?id=202

Copyright (c) Marimer LLC