我的课程是:
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;
}
我的表格:
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");
}
}
}
问题在这里:
这是我用来调用方法的按钮,它让我在Person数组中给出了每个人的年龄。但我知道这是错的。所以有人请指导我。
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();
}
}
我列出了我的问题所在。基本上我想做的就是调用方法并显示Person数组中每个人的所有年龄。有二十个人,我想展示他们的年龄。我相信我的代码非常适合在我的课程中使用我的方法,但我不确定是否为了使用该方法而创建了一个新实例。
答案 0 :(得分:4)
您可以使用LINQ来获取所有人的年龄。但是,由于您没有告诉我们您希望在何处显示此信息以及您希望如何加入所有年龄段,我假设您希望在TextBox(或其他位置)中将其显示为字符串,并将这些行拆分为{{1 }}:
Environment.NewLine
顺便说一下,这是计算年龄的更准确方法:
var allPersonAge = People.Select(p => p.GetAge());
richTxtDisplay.Text = string.Join(Environment.NewLine, allPersonAge);
答案 1 :(得分:2)
for (int i = 0; i < Person.Length; i++)
{
Person[i].getAge();
}
答案 2 :(得分:1)
foreach(Person person in People)
{
int age = person.getAge();
//Do something with the age
}
答案 3 :(得分:0)
如何使用LINQ
List<int> result = People.Select(p => p.GetAge()).ToList();