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, 22, 7);
birth[1] = new DateTime(1962, 15, 9);
birth[2] = new DateTime(1984, 21, 4);
birth[3] = new DateTime(1977, 24, 1);
birth[4] = new DateTime(1983, 12, 8);
birth[5] = new DateTime(1979, 14, 1);
birth[6] = new DateTime(1965, 19, 9);
birth[7] = new DateTime(1968, 21, 2);
birth[8] = new DateTime(1980, 22, 7);
birth[9] = new DateTime(1982, 20, 7);
birth[10] = new DateTime(1984, 19, 4);
birth[11] = new DateTime(1968, 11, 9);
birth[12] = new DateTime(1968, 21, 8);
birth[13] = new DateTime(1975, 5, 2);
birth[14] = new DateTime(1945, 15, 3);
birth[15] = new DateTime(1969, 14, 6);
birth[16] = new DateTime(1987, 141, 4);
birth[17] = new DateTime(1976, 23, 5);
birth[18] = new DateTime(1989, 28, 6);
birth[19] = new DateTime(1988, 23, 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)
{
try
{
for (int i = 0; i < People.Length; i++)
{
This is where I need help Displaying my array that I have populated through my form1 Load
displayMessage("Name: " + People[i].Firstname + People[i].Lastname + " BirthDate: " + People[i].Birthdate.Year + People[i].Birthdate.Day + People[i].Birthdate.Month + "\n\n");
//richTxtDisplay.AppendText(People[i].ToString());
//richTxtDisplay.AppendText(People[i].Firstname + People[i].Lastname + People[i].Birthdate + "\n");
}
}
catch
{ }
}
private void btnGetAge_Click(object sender, EventArgs e)
{
int Birth = int.Parse(textBox1.Text);
}
public void displayMessage(string message)
{
// Void Method
MessageBox.Show(message);
}
public void displayRichTxtMessAppendText(string message)
{
// Void Method
richTxtDisplay.AppendText(message);
}
}
And my class is Person..
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; } }
public int getAge()
{
TimeSpan ts =
DateTime.Now - _birthdate;
int year = (int)ts.TotalDays / 365;
return year;
}
public int daysUntillBirthDate()
{
int age = getAge();
DateTime proximobirthday = _birthdate.AddYears(age + 1);
TimeSpan ts = proximobirthday - DateTime.Now;
return ts.Days;
}
}
I have labeled where I am displaying my array with the first names, last names and birth date. I don't have any errors and it won't show anything and I can click the button to display as many times and it won't break my code. I can't remember which format exemption to use but the try catch should work good enough.
your initializing date is wrong
format should be year, month and day.
this will not work, correct that. that is the only issue.
Here is the constructor signature of DateTime