Dates probably don't need extra validation rules

Topics: Developer Forum
Dec 11, 2006 at 3:35 PM
I noticed you have these validation attributes:

ValidateDateOccursAfter
ValidateDateOccursBefore
ValidateDateOccursOnOrAfter
ValidateDateOccursOnOrBefore
- & -
ValidateGreaterThan
ValidateGreaterThanOrEqualTo
ValidateLessThan
ValidateLessThanOrEqualTo

Aren't these essentially the same? You're just seeing if a Date is greater than / less than / etc. to some other date right? I believe you can just convert a DateTime object into IComparable and just evaluate it that way.

for a DateOccursAfter '1/1/07'...

IComparable dt = (IComparable)DateTime.Now;
return (dt.CompareTo("1/1/07") > 0 ? true : false);

You could essentially do this same thing for any object that is IComparable, including int, string, double, etc. Just convert the ValidateGreaterThan to accept an IComparable instead of object, and accept a Type argument so you can convert strings into proper types (dates) if needed using the

Convert.ChangeType("1/1/07", typeof(DateTime)); etc.

~justin
Coordinator
Dec 11, 2006 at 6:07 PM
Yeah, I thought about that actually. I guess I decided against it because I wanted date validation to be very specific and thus called out appropriately. Maybe I'll revisit that decision.