我无法显示我通过form1Load填充的数组。将运行没有错误但不会运行

时间:2012-06-06 03:45:05

标签: c# csharpcodeprovider

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);
    }
}

我的班级是人..

 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应该足够好。

1 个答案:

答案 0 :(得分:2)

您的初始化日期错误

new DateTime(1987, 22, 7); // month can't be 22! 

格式应为年,月和日。

这不起作用,纠正。这是唯一的问题。

这是DateTime的构造函数签名

public DateTime(int year, int month, int day);