>Doesn't the regular StartDate==null will give the same result ?
>OTOH StartDate.Value is handier than having to case as in (DateTime)StartDateYeah, StartDate == null works too. I don't use nullable types, so I wasn't sure until I tested it. Much more readable. (Plus, my example was wrong ... .HasValue isn't a method, it's property).
~~Bonnie
>
>>When using Nullable types, you have to use the .Value property.
>>
>>
>>if (StartDate.HasValue())
>> StartDate.Value.Year ...etc.etc.
>>
>>
>>~~Bonnie
>>
>>
>>>>>Hi,
>>>>>
>>>>>I want to calculate the age in years so I have this code:
>>>>>
>>>>>
public static int CalculateAge(DateTime? StartDate, DateTime CurrentDate)
>>>>> {
>>>>>
>>>>> int YearsPassed = CurrentDate.Year - StartDate.Year;
>>>>> // Are we before the birth date this year? If so subtract one year from the mix
>>>>> if (CurrentDate.Month < StartDate.Month || (CurrentDate.Month == StartDate.Month && CurrentDate.Day < StartDate.Day))
>>>>> {
>>>>> YearsPassed--;
>>>>> }
>>>>> return YearsPassed;
>>>>> }
>>>>>
>>>>>I call this code like this:
>>>>>
>>>>>
int policyYear = CalculateAge(this.Entity.IssueDate, DateTime.Now);
>>>>>
>>>>>this.Entity.IssueDate could have a null value. Of course I get errors like this:
>>>>>
>>>>>
>>>>>
>>>>>I've tried putting an if {this.Entity.IssueDate != null} around my call to the function and making th eparameter not nullable, but that hasn't worked. How do I adjust this code to work?
>>>>
>>>>That would depend on what you want to happen when Entity.IssueDate is null........
>>>
>>>If it's null, then I don't want the calculation to be done.