-3
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.

Servy
  • 202,030
  • 26
  • 332
  • 449
thanatus
  • 1
  • 1
  • 2
  • "Will run without errors but wont run" - maybe because of `catch { }`? – Paul Bellora Jun 06 '12 at 04:00
  • 1
    What have you done to debug this? Have you tried putting any breakpoints in your code to see if it is even hitting the methods `btnDisAll_Click` and `Form1_Load`? Using the debugger, breakpoints and watches you should be able to solve this problem yourself - or at least narrow the problem down a bit. – StanK Jun 06 '12 at 04:02
  • 1
    And you should (almost) **NEVER** silently swallow (catch) exceptions. Unless you definitely know what you are doing. Exceptions are for a reason. By catching exceptions and doing nothing, you are destroying error messages that you should see. – Mohammad Dehghan Jun 06 '12 at 04:06
  • Im using exemption fe because I am desperate to find the problem and it brings up "Object reference not set to an instance of an object" and my break Points show an error saying to use the new keyword along with try catch. – thanatus Jun 06 '12 at 04:17
  • Thanks everyone for the help I appreciate it. – thanatus Jun 06 '12 at 04:18
  • When you get `NullReferenceException`, well, certainly there is something to fix. In this case, because your `People` elements are not initialized (read the answer below), the element are `null`. – Mohammad Dehghan Jun 06 '12 at 04:24

1 Answers1

2

your initializing date is wrong

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

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

public DateTime(int year, int month, int day);
Damith
  • 62,401
  • 13
  • 102
  • 153
  • I think this is the issue. Visual Studio swallows the exceptions thrown in the `Form.Load` event while debugging. If you hit the `Ctrl+F5` instead of `F5`, you will see the exception. – Mohammad Dehghan Jun 06 '12 at 04:20
  • Thanks so much I knew it was something dumbh I did lol and I don't know why I switched that around but your help saved me time. Thx and I'm new to this site but how do I give you kudo's do i just say yes when it asks me "was this answer useful?" – thanatus Jun 06 '12 at 04:33
  • @thanatus: You should mark the answer as the accepted answer. Please read the [faq](http://stackoverflow.com/faq). – Mohammad Dehghan Jun 06 '12 at 04:49