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++)
{
这是我需要帮助的地方 显示我通过 form1 填充的数组
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);
}
}
我的班级是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;
}
}
我已经用名字、姓氏和出生日期标记了显示数组的位置。我没有任何错误,它不会显示任何内容,我可以单击按钮多次显示,它不会破坏我的代码。我不记得要使用哪种格式豁免,但 try catch 应该足够好。