locked
Calling an Age method from a class RRS feed

  • Question

  • I'm having trouble understanding how I calculate Age in my Class.

    I understand the method on how it is calculated however I don't understand how I'm supposed to use it within my program.

    I have an abstract Class called Person and there are properties for Person, Name, DOB etc...Then I have 2 classes, Dentist and Patient who inherit from Person. My program then needs to calculate the age from the DOB which I have created this method(Which was researched). Its the use of this method I'm finding difficult.

    This is my Method:

            public int AgeInYears(DateTime bday)
            {
                DateTime now = DateTime.Today;
                int age = now.Year - bday.Year;
                if (bday.AddYears(age) > now)
                    age--;
                return age;
            }


    • Edited by GG84 Monday, April 18, 2016 8:50 PM
    Monday, April 18, 2016 8:49 PM

Answers

  • Greetings.

    Your question is a little vague, but I'll do my best to answer.

    If that method is not in the Person class, then you should pass it the date of birth of the person in question. Suppose you have a Dentist object called dentist1 and the date of birth in the Person class is called DOB. You could get that dentist's age like so..

    int age = AgeInYears(dentist1.DOB);

    But a better idea would probably be to move the AgeInYears method into the Person class and change it so that instead of taking a birthday as an argument, it uses the DOB from the class. Then you would call it like so...

    int age = dentist1.AgeInYears();

    I won't show you how to re-write the method to do this. I'll leave that to you if you want to try that path.

    • Proposed as answer by User3DX Tuesday, April 19, 2016 12:03 AM
    • Marked as answer by Kristin Xie Thursday, April 28, 2016 1:52 AM
    Monday, April 18, 2016 11:27 PM
  • public abstract Person
    {
        // with your method
    }
    
    public class Someclass : Person
    {
        //some other code too
    }
    
    class Program 
    { 
        public void Main() 
        { 
          Person person = new Someclass(); 
          var years = person.AgeInYears(somedate) ; 
        } 
    }
    

    • Proposed as answer by Kristin Xie Wednesday, April 20, 2016 6:12 AM
    • Marked as answer by Kristin Xie Thursday, April 28, 2016 1:52 AM
    Tuesday, April 19, 2016 3:10 AM

All replies

  • Greetings.

    Your question is a little vague, but I'll do my best to answer.

    If that method is not in the Person class, then you should pass it the date of birth of the person in question. Suppose you have a Dentist object called dentist1 and the date of birth in the Person class is called DOB. You could get that dentist's age like so..

    int age = AgeInYears(dentist1.DOB);

    But a better idea would probably be to move the AgeInYears method into the Person class and change it so that instead of taking a birthday as an argument, it uses the DOB from the class. Then you would call it like so...

    int age = dentist1.AgeInYears();

    I won't show you how to re-write the method to do this. I'll leave that to you if you want to try that path.

    • Proposed as answer by User3DX Tuesday, April 19, 2016 12:03 AM
    • Marked as answer by Kristin Xie Thursday, April 28, 2016 1:52 AM
    Monday, April 18, 2016 11:27 PM
  • public abstract Person
    {
        // with your method
    }
    
    public class Someclass : Person
    {
        //some other code too
    }
    
    class Program 
    { 
        public void Main() 
        { 
          Person person = new Someclass(); 
          var years = person.AgeInYears(somedate) ; 
        } 
    }
    

    • Proposed as answer by Kristin Xie Wednesday, April 20, 2016 6:12 AM
    • Marked as answer by Kristin Xie Thursday, April 28, 2016 1:52 AM
    Tuesday, April 19, 2016 3:10 AM