Thursday, May 8, 2014

Class with Date & Time (Lab)


namespace Zau_Class_with_DateTime_Lab
{

    public class Person
    {
        //private fields
        private string _firstname;
        private string _lastname;
        private DateTime _dateofbirth;

        //constructor
        public Person (string firstname, string lastname, DateTime dateofbirth)
        {
            _firstname = firstname;
            _lastname = lastname;
            _dateofbirth = dateofbirth;
        }

        //properties
        public string FirstName
        { get { return _firstname; } }
        public string LastName
        { get { return _lastname; } }
        public DateTime DateOfBirth
        { get { return _dateofbirth; } }

    }
}
//============================================
namespace Zau_Class_with_DateTime_Lab
{
    public partial class Form1 : Form
    {
        List<Person> personList = new List<Person>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Person
            person = new Person("Zau", "Bawk", new DateTime(1984, 9, 23));
            personList.Add(person);
            person = new Person("David", "Lama", new DateTime(2011, 7, 2));
            personList.Add(person);
            person = new Person("Grace", "Lama", new DateTime(2003, 11, 25));
            personList.Add(person);
            person = new Person("Seng", "Lahtaw", new DateTime(1988, 09, 19));
            personList.Add(person);
            person = new Person("Obama", "Barack", new DateTime(1955, 8, 22));
            personList.Add(person);
            person = new Person("George", "Bush", new DateTime(2011, 3, 13));
            personList.Add(person);
            person = new Person("Bill", "Clinton", new DateTime(1930, 4, 25));
            personList.Add(person);

        }

        private void btnDisplayNameList_Click(object sender, EventArgs e)
        {
            DisplayPerson();
        }
        private void DisplayPerson()
        {
            listView1.Items.Clear();

            foreach (Person person in personList)
            {
                ListViewItem lvi = new ListViewItem(person.FirstName);
                lvi.SubItems.Add(person.LastName);
                lvi.SubItems.Add(person.DateOfBirth.ToShortDateString());

                listView1.Items.Add(lvi);
            }
        }
    }
}
///Lab Assignment
///Create a class Person with _firstname,
///_lastname, _dateOfBirth (of type DateTime)
///Add constructor and properties
///
///in Form1: create a List of Person objects
///in Form1-load: add at least 10 people to the
///list
///Add a listview and a button
///Display the content of the list in a listview

No comments:

Post a Comment