//Person Class
namespace DotNet_PredefinedDelegates
{
public class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public DateTime DateOfBirth { get; set; }
public override string ToString()
{
return Firstname + " " + Lastname;
}
}
}
//==================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DotNet_PredefinedDelegates
{
public partial class Form1 : Form
{
/// <summary>
/// .Net Generic Delegates
/// Action<> delegates // All action delegates return a void
/// read more... http://msdn.microsoft.com/en-us/library/system.action(v=vs.110).aspx
/// Func<> delegates // All the Func delegates return a value of some type
/// Predicate<> delegate //takes a single parameter of some type, but always returns a boolean
/// </summary>
List<int> intList = new List<int>();
List<Person> personList = new List<Person>();
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Initialize intList
for (int i = 1 ; i <= 50; i++)
intList.Add(rand.Next(-99, 100));
//initialize personList
Person p = new Person { Firstname = "John", Lastname = "Doe", DateOfBirth = new DateTime(1990, 11, 2) };
personList.Add(p);
p = new Person { Firstname = "Karen", Lastname = "Jordan", DateOfBirth = new DateTime(1979, 2, 23) };
personList.Add(p);
p = new Person { Firstname = "Amy", Lastname = "Johnson", DateOfBirth = new DateTime(1980, 8, 15) };
personList.Add(p);
p = new Person { Firstname = "Seng", Lastname = "Mary", DateOfBirth = new DateTime(1988, 4, 30) };
personList.Add(p);
p = new Person { Firstname = "David", Lastname = "Lama", DateOfBirth = new DateTime(2011, 7, 2) };
personList.Add(p);
p = new Person { Firstname = "Grace", Lastname = "Lama", DateOfBirth = new DateTime(2013, 11, 25) };
personList.Add(p);
p = new Person { Firstname = "Zau", Lastname = "Bawk", DateOfBirth = new DateTime(1985, 3, 17) };
personList.Add(p);
Display2(personList);
}
private void Display<T>(List<T> list)
{
richTextBox1.Clear();
foreach (T num in list)
richTextBox1.AppendText(num + "\n");
}
//=======================================
private void Display2(List<Person> list)
{
richTextBox1.Clear();
foreach (Person p in list)
{
richTextBox1.AppendText(String.Format(
"{0} {1} {2}", p.Firstname, p.Lastname,
p.DateOfBirth.ToShortDateString() + "\n"));
}
}
private void btnGetOddValues_Click(object sender, EventArgs e)
{
//use the FindAll method to return all the odd values
//eg. read more... http://msdn.microsoft.com/en-us/library/vstudio/fh1w7y8z(v=vs.100).aspx
//public List<T> FindAll(Predicate<T> match)
//The predicate delegate object match should be referencing a method that has the same
//signature as the Predicate<T> delegate type
//since the List is of type int, the T is int
//The FindAll method will call the predicate (in this case the match object for
//every element int he list, then saves and returns
//only the element for which the match returns true.
//1. Create Predicate <int> delegate object
//Predicate<int> predicate = x => x % 2 != 0;
//List<int> oddList = intList.FindAll(predicate);
//or
List<int> oddList2 = intList.FindAll(x => x % 2 != 0);
//every element of the intList will be tested against the
//condition stated in the predicate, if the result is true
//then the element is kept and returned
Display(oddList2);
}
private void btnFindnameWith4Letter_Click(object sender, EventArgs e)
{
//Get people with 4-Letter firstname
Predicate<Person> predicate = p => p.Firstname.Length == 4;
List<Person> shortfirstnamepeople = personList.FindAll(predicate);
//Display<Person>(shortfirstnamepeople);
Display2(shortfirstnamepeople);
}
private void btnGetPersonWithGivenBirthYear_Click(object sender, EventArgs e)
{
int birthYr;
int.TryParse(txtBirthYear.Text, out birthYr);
Predicate<Person> predicate = p => p.DateOfBirth.Year == birthYr;
List<Person> personsWithGivenBirthYear = personList.FindAll(predicate);
Display2(personsWithGivenBirthYear);
}
private void btnGetPersonWithLetter_Click(object sender, EventArgs e)
{
string lastInitial = txtLastNameInitial.Text.ToUpper();
Predicate<Person> predicate = p => p.Lastname.ToUpper().StartsWith(lastInitial);
List<Person> personWithLastInitial = personList.FindAll(predicate);
Display2(personWithLastInitial);
}
private void btnDisplayAllPersons_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
Display2(personList);
}
}
}
///Lab: (due Thursday 1/15)
///Add more person object to the personList
///Add button to get / display all the people born in a given year
///(get the year from the user)
///Add button to get / display all the people whose last name start with a given letter (read from user).

No comments:
Post a Comment