SmartDate not honoring FormatString?

SmartDate not honoring FormatString?

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


RandyH posted on Wednesday, May 12, 2010

I have the following property defined:

Private Shared _ApptTimeProperty As PropertyInfo(Of SmartDate) = RegisterProperty(Of SmartDate)(Function(x) CType(x, PatientAppointmentTable).ApptTime, "ApptTime", New SmartDate() With {.FormatString = "G"})

    Public Property ApptTime() As System.String Implements IPatientAppointmentTableBase.ApptTime

        Get

            Return (GetPropertyConvert(Of SmartDate, String)(_ApptTimeProperty))

        End Get

        Set(ByVal value As System.String)

            SetPropertyConvert(Of SmartDate, String)(_ApptTimeProperty, value)

        End Set

    End Property

The date always returns using the "d" format and never the "G" format that is being set when the property is being registered. What am I missing??

RockfordLhotka replied on Wednesday, May 12, 2010

This probably has to do with SmartDate being a value type, not a reference type. If you ever set the value to a new SmartDate (which happens in the setter of course) then you'd lose the FormatString value, because the property is now using a completely different SmartDate instance than the default one you provided.

This is one of those quirky things that happens when a value type has properties - they start to feel like a reference type, but they really aren't...

RandyH replied on Wednesday, May 12, 2010

So I changed the getter from :

            Return GetPropertyConvert(Of SmartDate, String)(_ApptTimeProperty)

to:

            Dim value = GetProperty(Of SmartDate)(_ApptTimeProperty)

            Return value.ToString(_ApptTimeProperty.DefaultValue.FormatString)

Does this seem like the right way to handle it?

RockfordLhotka replied on Monday, May 17, 2010

Unless you can set the static default format string for all SmartDate instances, yes.

RandyH replied on Tuesday, May 18, 2010

No, I can't set the static default I need the other dates to format with "d".

 

Thanks for the help

JonnyBee replied on Tuesday, May 18, 2010

Hi Randy,

What version of CSLA are you using? 

Check if Utilities.CoerceValue handles SmartDate in a specific way.  In later versions there is special handling to only update the value on existing instance rather than assigning a new SmartDate instance to the backing field.

\Core\TypeConverters\SmartDateConverter handles default converting to/from string and datetime values.

RandyH replied on Tuesday, May 18, 2010

I am using version 3.7.

Copyright (c) Marimer LLC