I have a property DateOfBirth
and a property Age
.
DateOfBirth
is DateTime
datatype and Age
is int
datatype.
I want to calculate the person's age inside the constructor, and I have:
private int CalculateAge(DateTime birthDate, DateTime now)
{
int age = now.Year - birthDate.Year;
if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
{
age--;
}
return age;
}
public virtual DateTime? Dob { get; set; }
public virtual int Age { get; set; }
public MyObject()
{
Age = CalculateAge(Dob, DateTime.Now);
}
At compile time I'm getting the following errors:
The best overloaded method match for ... has some invalid arguments
and
cannot convert from 'System.DateTime?' to System.DateTime