Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8045217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:28:41+00:00 2026-06-05T05:28:41+00:00

My class is: public class Person { private string _firstname; private string _lastname; private

  • 0

My class is:

public class Person
{
    private string _firstname;
    private string _lastname;
    private DateTime _birthdate;

    public Person(string firstname, string lastname, DateTime birthdate)
    {
        _firstname = firstname;
        _lastname = lastname;
        _birthdate = birthdate;
    }

    public string Firstname
    { get { return _firstname; } }

    public string Lastname
    { get { return _lastname; } }

    public DateTime Birthdate
    { get { return _birthdate; } }

Here is my method I am accessing in order to get everyone’s age:

public int getAge()
{
    TimeSpan ts =DateTime.Now - _birthdate;
    int year = (int)ts.TotalDays / 365;
    return year;
}

My form:

namespace May22_StructClassObj_HW
{
    public partial class Form1 : Form
    {
        DateTime[] birth = new DateTime[20];
        Person[] People = new Person[20];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a class Person with the following fields _firstname, _lastname, _birthDate(DateTime Type) Add constructor, properties (get only) and a method GetAge that returns the age (int) of a person. 
            // In Form1, Create an array of Person objects to hold 20 people
            // In Form1_Load: Populate the array with 20 Person objects

            // Add Gui to display all the people in the list (first and last names, birthdate, and age
            // Add Gui 
            //people[0] = new Person("John","Stockton", DateTime.)

            string[] first = new string[20] { "Scott", "Ramona", "Todd", "Melissa", "Naomi", "Leland", "Conor", "Julie", "Armondo", "Leah", "Frank", "Peter", "Ila", "Mandy", "Sammy", "Gareth", "Garth", "Wayne", "Freddy", "Mark" };

            string[] last = new string[20] { "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Carrel", "MaloyTheBeautiful", "Johnson", "Smith", "Sinatra", "Clemens", "Eels", "Johnson", "Eels", "Thompson", "Brooks", "World", "Crugar", "Thomas" };

            birth[0] = new DateTime(1987, 2, 7);
            birth[1] = new DateTime(1962, 5, 9);
            birth[2] = new DateTime(1984, 1, 4);
            birth[3] = new DateTime(1977, 4, 1);
            birth[4] = new DateTime(1983, 2, 8);
            birth[5] = new DateTime(1979, 4, 1);
            birth[6] = new DateTime(1965, 9, 9);
            birth[7] = new DateTime(1968, 1, 2);
            birth[8] = new DateTime(1980, 2, 7);
            birth[9] = new DateTime(1982, 2, 7);
            birth[10] = new DateTime(1984, 12, 4);
            birth[11] = new DateTime(1968, 11, 9);
            birth[12] = new DateTime(1968, 2, 8);
            birth[13] = new DateTime(1975, 5, 2);
            birth[14] = new DateTime(1945, 5, 3);
            birth[15] = new DateTime(1969, 4, 6);
            birth[16] = new DateTime(1987, 1, 4);
            birth[17] = new DateTime(1976, 3, 5);
            birth[18] = new DateTime(1989, 8, 6);
            birth[19] = new DateTime(1988, 2, 9);

            // Populate Array Person[] People = new Person[20];   
            for (int i = 0; i < People.Length; i++)
            {
                People[i] = new Person(first[i], last[i], birth[i]);
            }

        }

        private void btnDisAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < People.Length; i++)
            {
                richTxtDisplay.AppendText("Name: " + People[i].Firstname + "\t" + People[i].Lastname + "\t" + " BirthDate: " + People[i].Birthdate + "\n\n");
                //richTxtDisplay.AppendText(People[i].ToString());
                //richTxtDisplay.AppendText(People[i].Firstname + People[i].Lastname + People[i].Birthdate + "\n");
            }
        }
    }

Problem here:

Here is my button that I am using to call the method and have it give me the age of everyone in my Person array. But I know this is wrong. So someone please guide me through it.

private void btnGetAge_Click(object sender, EventArgs e)
{
    for (int i = 0; i < People.Length; i++)
    {
        Person Per = 
            new Person(People[i].Firstname, People[i].Lastname, People[i].Birthdate);
        Per.getAge();
    }
}

I have listed where my problem is. Basically all I want to do is call the method and display all the ages of everyone in my Person array. There are twenty people and I want to show their age. I do believe my code is perfect to get the age in my method in my class but I’m unsure whether I created a new instance right in order to use the method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:28:43+00:00Added an answer on June 5, 2026 at 5:28 am
    for (int i = 0; i < Person.Length; i++)
    {
       Person[i].getAge();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Person { private: string firstName; string lastName; public: Person() {} Person(ifstream &fin) {
Given a simple class: public class Person { public string FirstName; public string LastName;
Please consider the following code: public class Person ( public string FirstName {get; set;}
I have two classes: public class Person { public string FirstName { get {
Example-I have a person class Public Class Person Private _fname As String Public Property
I have a model public class PersonModel : INotifyPropertyChanged { private string _firstname; public
Let's say I have a class like this: public class Person { private String
Im facing an issue with The following: public class Person { private long id;
Given a class such as: class Person { private: char *name; public: Person() {
For the sake of argument, here's a simple person class public class Person :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.