SmartDate GetProperty

SmartDate GetProperty

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


PederSvaleng posted on Thursday, September 03, 2009

Hi, there.

I trying the most simple thing here, but I'm giving up.. What do I have to do to change the format of this SmartDate. I just want to do it for this property and not for the entire app. Lets say I just want to show the day :)

Thanks in advance

private static PropertyInfo FromDateProperty =
RegisterProperty(c => c.FromDate);

public string FromDate
{
get
{
return GetPropertyConvert(FromDateProperty);

}
set
{
SetPropertyConvert(FromDateProperty, value);
}
}

JonnyBee replied on Thursday, September 03, 2009

Hi,

Write your getter like this:

private static PropertyInfo FromDateProperty = RegisterProperty(c => c.FromDate);
public string FromDate
{
     get
    {
        return GetProperty(FromDateProperty).ToString("dd");
    }
}

In your example it doesn't make sense to have a setter. If you wish - then use the value from the user  to create a valid datestring (or a string that SmartDate can parse, like t=today, +=tomorrow, -=yesterday, etc) and use
SetPropertyConvert(FromDateProperty, myFormattedValue);

/jonnybee

RockfordLhotka replied on Thursday, September 03, 2009

That is a good solution.

Or in your RegisterProperty() call use an overload that let's you specify the default value of the property, which would be a SmartDate object. As you create the SmartDate object you can initialize it (to control whether min or max means empty, and to override the default format string for SmartDate).

PederSvaleng replied on Friday, September 04, 2009

Thanks to both of you. The solution from jonnybee worked.

Rocky.. How do I override the default format string. Do I need a private backing field? I think I'm a bit slow here :)


private static PropertyInfo FromDateProperty =
RegisterProperty(c => c.FromDate, "FromDate", new SmartDate(DateTime.Now));

public string FromDate
{
get
{
return GetPropertyConvert(FromDateProperty);

}
set
{
SetPropertyConvert(FromDateProperty, value);
}
}

JonnyBee replied on Friday, September 04, 2009

Hi,

Register property with this command:

private static PropertyInfo FromDateProperty = RegisterProperty(c => c.FromDate, "FromDate", new SmartDate(DateTime.Now){FormatString = "dd.MM.yyyy"});

/jonnybee

PederSvaleng replied on Sunday, September 06, 2009

Smooth! Thanks again..

JoeFallon1 replied on Thursday, November 12, 2009

JonnyBee:
Hi,

Register property with this command:

private static PropertyInfo FromDateProperty = RegisterProperty(c => c.FromDate, "FromDate", new SmartDate(DateTime.Now){FormatString = "dd.MM.yyyy"});

/jonnybee

Out of curiosity, how would you write the above in VB?

Public Shared FromDateProperty As PropertyInfo(Of SmartDate) = RegisterProperty(Of SmartDate)(Function(x) x.FromDate)

I got this far but how do you set the Format String?

Joe

 

ajj3085 replied on Friday, November 13, 2009

I don't know VB.. but when I need to convert, sometimes I compile the C#, then use Reflector.Net to get the VB version of the code.  The latest version does a really nice job of generating code you'd actually type.

JoeFallon1 replied on Friday, November 13, 2009

Rocky,

Can you please respond to my question?

Thanks.

Joe

 

RockfordLhotka replied on Friday, November 13, 2009

Fortunately Bing is your friend…

 

The concept being used in the code (regardless of language) is a type initializer.

 

    Private Shared StartedProperty As PropertyInfo(Of SmartDate) = _

      RegisterProperty(Of SmartDate)(Function(c) c.Started, "Started", _

                                     New SmartDate(Now) With {.FormatString = "yyyy"})

 

 

JoeFallon1 replied on Friday, November 13, 2009

Bing only helps if you can remember the key words you are looking for. <g>

LOL.

Thanks! I see how to do it now.

Joe

 

mtavares replied on Tuesday, December 15, 2009

Just curious, will setting the formatstring in the default value of the register propery only work when working with a newly created date? Because I used the Type Initializer technique, but when performing a fetch and loading the property via:

LoadProperty(SampleDateProperty, dr.GetSmartDate("SampleDate"))

the format reverted back to the short date string default. Should the property be loaded differently at that point in order to keep the format string?

RockfordLhotka replied on Tuesday, December 15, 2009

The only way to globally set the default format is to use the static default
format on the SmartDate type itself (make sure to do this on both client and
server).

Otherwise the format value is per-instance of the type. SafeDataReader
creates a new SmartDate when you call GetSmartDate - it has no way of
knowing what other formats you might have set for other SmartDate values in
your app.

If you allow type coercion to work you _might_ have success:

using (BypassPropertyChecks)
{
SampleDate = dr.GetDateTime("SampleDate");
}

This is because CSLA will attempt to update the existing SampleDate property
value to match the newly supplied DateTime value, thus preserving existing
formatting of the existing value. But if you do this you may lose some of
the automatic empty value processing - I'm not really sure. Try it and see
if it works.

RockfordLhotka replied on Friday, November 13, 2009

I would if I knew the answer…

 

Copyright (c) Marimer LLC