//a more typical way to create employee objects private void button3_Click(object sender, EventArgs e) { Employee objE1 = new Employee(); objE1.EmployeeId = 1; objE1.Name = "Tim Thomas"; objE1.DOB = DateTime.Parse("1/1/1980"); objE1.Gender = Gender.Male; MessageBox.Show(objE1.GetData()); Employee objE2 = new Employee(2, "Jill James", DateTime.Parse("1/2/1990"), Gender.Female); MessageBox.Show(objE2.GetData()); }
// Creating an array of objects with a parent class variable private void button4_Click(object sender, EventArgs e) { Customer objC1 = new Customer(); objC1.CustomerId = 1; objC1.Name = "Sue Jones"; objC1.DOB = DateTime.Parse("1/1/1980"); objC1.Gender = Gender.Female; Employee objE1 = new Employee(); objE1.EmployeeId = 1; objE1.Name = "Tim Thomas"; objE1.DOB = DateTime.Parse("1/1/1980"); objE1.Gender = Gender.Male; Person[] arrPersons = { objC1, objE1 }; foreach (Person item in arrPersons) { MessageBox.Show(item.GetData()); } }
static void FullName(Employee name) { Console.WriteLine("{0} {1}",name.FirstName,name.LastName); Debug.Assert(name != null, name.FirstName, name.LastName); }
public override bool Equals(object obj) { Employee employee = obj as Employee; return(employee != null && ID.Equals(employee.ID)); }
//Inheritance and objects private void button1_Click(object sender, EventArgs e) { try { Person objP1; /* You cannot make a person object when the class is marked abstract. */ //objP1 = new Person(); /* But, can you make a variable of the abstract class that points to a object? */ objP1 = new Employee(); //or objP1 = new Customer(); //This connects the event to a method objP1.NameChanged += new EventHandler(objP1_NameChanged); //Set the properties objP1.Name = "Bob Smith"; objP1.DOB = Convert.ToDateTime("01/01/1980"); objP1.Gender = Gender.Male; //Use a method MessageBox.Show(objP1.GetData()); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
static void Main(string[] args) { var Hr = new Employee[] { new FullTimeEmployee {FirstName="Mike", LastName="Harrison", Years= 5 }, new ContractEmployee {FirstName="Sally",LastName="Williams", Months = 2}, }; foreach (var name in Hr) { FullName(name); } Console.ReadLine(); }