-1

Currently I am trying to calculate the age of someone with a given birthdate, but whenever I run my code the birthdate refactors to the default 01-01-0001.

Birthdate gets set here:

private DateTime BirthDate { get; set; }

This is how i calculate the age of the person:

public int Age
    {
        get
        {
            int Age = (int)(DateTime.Today - BirthDate).TotalDays;
            Age = Age / 365;
            return Age;
        }
    }

This is the Birthdate I am trying to use:

new DateTime(2000, 3, 12)

But whenever I use the debugger in Visual Studio it gives the birthdate: 01-01-0001

EDIT: This is the rest of the code where I am using the DateTime: This part is in my main class:

Customer cust = new Customer();

        cust.VoegToe(new Customer("Lionel Messi", new DateTime(2000, 3, 12), new DateTime(2019, 2, 23)));

This part is in my sub Class:

public Customer()
            {
                customers = new List<Customer>();
            }




        public Customer(string name, DateTime BirthDate, DateTime signUpDate)
        {
            this.name = name;
            this.signUpDate = signUpDate;
            this.BirthDate= BirthDate;
        }

Is there a way to fix this?

Thanks in advance

Jordy Pouw
  • 11
  • 3
  • Did you assign the date to the `BirthDate` property? – Xiaoy312 Mar 08 '19 at 21:43
  • If I do: `BirthDate = new DateTime(2000, 3, 12); int age = Age;`, then `age` is `19`. Perhaps you should show the code that isn't working... – Rufus L Mar 08 '19 at 21:45
  • yes i have assigned it to a property, I am using a list to store the `BirthDate` – Jordy Pouw Mar 08 '19 at 21:46
  • Also, your `Age` property can be simplified to: `public int Age => (int)(DateTime.Today - BirthDate).TotalDays / 365;` – Rufus L Mar 08 '19 at 21:48
  • Could you include the actual code that sets the Birthdate property? The code in the question doesn't recreate the error. – St. Pat Mar 08 '19 at 21:51
  • post a complete working example of code. – elgaspar Mar 08 '19 at 21:58
  • You are creating a first `Customer` object with the parameterless constructor (i.e. no BirthDate), and then creating a second `Customer` object with the desired BirthDate. I'm assuming the `VoegToe` (Dutch for "add" according to Google) is trying to add to the List of Customers created by the parameterless constructor. Are you trying to get the `Age` of the first object, which contains the second object that has the actual Birthdate? – St. Pat Mar 08 '19 at 22:01
  • @St.Pat yes `VoegToe` is `add` forgot to translate that, the first time im calling `Customer` it should be reffering to the Class Customer and the second time it should be reffering to the constructor `Customer` – Jordy Pouw Mar 08 '19 at 22:07
  • 1
    They are both Constructors, and each one creates a new, separate object when called. They both refer to the class, the only difference is what values you set. – St. Pat Mar 08 '19 at 22:08
  • The first related link to this question is this one: https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c?rq=1. It has many suggestions – Flydog57 Mar 08 '19 at 22:10
  • @Flydog57 The problem isn't with getting the correct number for the age, it's that the BirthDate property of the object he's accessing doesn't have the value he expects it to have – St. Pat Mar 08 '19 at 22:12
  • Yeah, but he also has `Age = Age / 365;` Once he figures out that he needs to set the property properly, he should really read that Q&A to get an idea of how to do date/time calculations properly. This is a comment, not an answer. – Flydog57 Mar 08 '19 at 22:16
  • A Customer class that is both a single customer and the list of customers - that is an unexpected design choice. Why not create a Customers (note the plural) class for the list? – Hans Kesting Mar 09 '19 at 07:15

2 Answers2

0

I just tested the code you provided and it works just fine with 1 customer. The problem you have is probably around the list and having the class Customer containing a list of customers and how you access the age of instantiated objects.

This works just fine:

    public class Customer
    {
        private string name;
        private DateTime signUpDate;

        public DateTime BirthDate { get; }

        public int Age
        {
            get
            {
                int Age = (int)(DateTime.Today - BirthDate).TotalDays;
                Age = Age / 365;
                return Age;
            }
        }

        public Customer(string name, DateTime BirthDate, DateTime signUpDate)
        {
            this.name = name;
            this.signUpDate = signUpDate;
            this.BirthDate = BirthDate;
        }
    }

With a button to calculate the age and show:

        private void button1_Click(object sender, EventArgs e)
        {
            Customer test = new Customer("Lionel Messi", new DateTime(2000, 3, 12), new DateTime(2019, 2, 23));

            teAge.Text = test.Age.ToString();
        }

If you want to post exactly how you are accessing the age maybe I can help you more.

Matt
  • 319
  • 2
  • 7
0

Well, your logic is flawed. Most importantly, as it does not take leap years into account. The other thing is, that the DateTime class gives you all you need out of the box.

To make it testable, obviously, you should also try to make the given day as parameter:

static class AgeCalculator
{
    static int GetAgeInYears(DateTime birth) => GetAgeInYears(brith, DateTime.Today);

    static int GetAgeInYears(DateTime birth, DateTime mes)
    {
        var age = mes.Year - birth.Year - 1;
        // if the birthday already has past (or is today)
        if(birth.Month > mes.Month || (birth.Month == mes.Month && birth.Day => mes.Day)
        {
            age++;
        }
        return age;
    }

A more advanced approach would be create a DateTimeSpan class/struct, that also keeps track on the amount of days (and or months) of the age. But in most cases that is not part of the use case.

Corniel Nobel
  • 421
  • 4
  • 12