-1

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

CarenRose
  • 1,266
  • 1
  • 12
  • 24
panjo
  • 3,467
  • 11
  • 48
  • 82

9 Answers9

3

The best overloaded method match for .... has some invalid arguments and cannot convert from 'System.DateTime?' to System.DateTime

So what did you try to solve this? The error is pretty clear: you're passing a System.DateTime? parameter to a function that accepts a System.DateTime.

To fix it, either change the method signature

CalculateAge(DateTime? birthDate, DateTime now)
{
    if (!birthDate.HasValue)
    {
        return -1; // ?
    }
}

But as you see, that's quite useless. So change the call:

if (Dob.HasValue)
{
    Age = CalculateAge(Dob.Value, DateTime.Now);
}

Ultimately you'd just want to use a property for this:

public virtual int Age { 
    get
    {
        if (!Dob.HasValue)
        {
            throw new Exception(); // ?
            return -1; // ?
        }

        return CalculateAge(Dob.Value);
    }
}

As you see it doesn't matter where you solve this: you just have to check somewhere whether the nullable (?) date of birth contains a value.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
2

You should pass a DateTime not a nullable DateTime

Age = CalculateAge((Dob.HasValue ? Dob.Value : DateTime.Now), DateTime.Now);

Or change the receiving method

private int CalculateAge(DateTime? birthDate, DateTime now)

and apply all the check needed to avoid NullReferenceExceptions

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Or possibly change Age to be a nullable as well. Makes sense that the age is undefined if date of birth is undefined instead of returning age 0 when date of birth is unknown. – Vegar Jun 06 '13 at 10:39
  • 1
    @Vegar Yes there is some debatable logic here, but the immediate cause of the problem is the nullable DateTime – Steve Jun 06 '13 at 10:41
1

You CalculateAge method accepts a DateTime parameter, and you are passing it a DateTime? (nullable DateTime). You must change one of these, or cast to a DateTime.

Futhermore, there is no real reason for the second parameter, as DateTime.Now can be calculated inside the method.

Thirdly, see similar questions on SO for calculating age: Calculate age in C#

Community
  • 1
  • 1
Rotem
  • 21,452
  • 6
  • 62
  • 109
1

Look at your method declaration

private int CalculateAge(DateTime birthDate, DateTime now)

And DateOfBirth declaration

public virtual DateTime? Dob { get; set; }

You cannot use nullable DateTime property as a first parameter. Change declaration to

private int CalculateAge(DateTime? birthDate, DateTime now)

or remove nullability from Dob property

public virtual DateTime Dob { get; set; }
Piotr Czarnecki
  • 1,688
  • 3
  • 14
  • 22
0

You can use

public static int GetAge(DateTime birthDate)
{
DateTime n = DateTime.Now; // To avoid a race condition around midnight
int age = n.Year - birthDate.Year;

if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day))
age--;

return age;
}
kostas ch.
  • 1,960
  • 1
  • 17
  • 30
0
use private int CalculateAge(DateTime? birthDate, DateTime now)

instead of

private int CalculateAge(DateTime birthDate, DateTime now)
Taj
  • 1,718
  • 1
  • 12
  • 16
0

Use TimeSpan to get the difference between the two dates as mentioned here:

private int CalculateAge(DateTime birthDate, DateTime now)
{
   TimeSpan span = now.Subtract(birthDate);     
   return (int)span.TotalDays / 365;  
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Change method definition and check if birthDate has value (is not null)

private int CalculateAge(DateTime? birthDate, DateTime now)
{
   if(birthDate.HasValue)
   {
      int age = now.Year - birthDate.Year;
      if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
      {
         age--;
      }            
      return age;
   }
   else 
      return 0;
}
Nick
  • 4,192
  • 1
  • 19
  • 30
0

You will have to cast your DateTime? to DateTime like so

(DateTime)Dob

But why bother making Dob nullable in the first place if you are not handling the possibility of a null date anywhere in your code?

CodeCamper
  • 6,609
  • 6
  • 44
  • 94