-3

I am doing a tutorial series on .Net Academy and have run into a problem. They don't really have a useful help section so I was wondering if anyone here can help me. My issue is that I think I have done the code right, but I get this error:

Not all requirements have been met.
You must declare a DateTime property named DateOfBirth

Here is my code:

using System;

public class Person
{
    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int Age {get; set;}

    public string GetFullName()
    {
        return FirstName + " " + LastName;
    }

    public int DateOfBirth()
    {
        return DateTime.Now.AddYears(-Age).Year;
    }
}
public class Program
{
    public static void Main()
    {

    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
SpyrosKo
  • 167
  • 1
  • 11

3 Answers3

3

This:

public int DateOfBirth()
{
    return DateTime.Now.AddYears(-Age).Year;
}

is not a property, but a method. You can easily turn it into a property though:

public int DateOfBirth
{
    get { return DateTime.Now.AddYears(-Age).Year; }
}

See the documentation for a description of properties in C#

You'll likely still run into issues since the type is int rather than the required DateTime. Skip the .Year reference and return the entire DateTime struct, and finally update the property signature:

public DateTime DateOfBirth
{
    get { return DateTime.Now.AddYears(-Age); }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
2

You should declare DateOfBirth as a property like this

public DateTime DateOfBirth {get; set;}

and age should be a caculated property instead like this

 public int Age 
        {
            get
               {
                return DateTime.Now.Year - DateOfBirth.Year;
               }    
        }
ClearLogic
  • 3,616
  • 1
  • 23
  • 31
  • Thank you @ClearLogic for helping me. That worked. I forgot to add what the assignment was: "Add a DateOfBirth property to your class, which is of type DateTime." I only needed the first part but seeing the second part too helped me understand even further. Thanks :) – SpyrosKo Aug 02 '17 at 16:20
  • 1
    Similarly you can convert `GetFullName` method into a getter only property `FullName` – ClearLogic Aug 02 '17 at 16:27
  • 1
    Of course if you change `Age` to be a calculated property (which it should be, since it changes but dateofbirth never changes), then you'll need to change `DateOfBirth` to a regular (non-calculated) property. Otherwise you'll have both properties trying to calculate their value from the other and have an infinite loop. – Rufus L Aug 02 '17 at 16:28
1

You have a method called DateOfBirth, instead it should be a property like this:

public DateTime DateOfBirth
{
        get { return DateTime.Now.Date.AddYears(-Age); }
}

Notice the usage of .Date after now to get the Date and not the time part.

dariogriffo
  • 4,148
  • 3
  • 17
  • 34