The implementation of Employee custom collection will be,
public class EmployeeCollection :
System.Collections.CollectionBase
{
public EmployeeCollection()
{
//
// TODO: Add constructor logic here
//
}
public Employee this[int index]
{
get { return (Employee)this.List[index]; }
set { this.List[index] = value; }
}
public int IndexOf(Employee item)
{
return base.List.IndexOf(item);
}
public int Add(Employee item)
{
return this.List.Add(item);
}
public void Remove(Employee item)
{
this.InnerList.Remove(item);
}
public void CopyTo(Array array, int index)
{
this.List.CopyTo(array, index);
}
public void AddRange(EmployeeCollection collection)
{
for (int i = 0; i < collection.Count; i++)
{
this.List.Add(collection[i]);
}
}
public void AddRange(Employee[] collection)
{
this.AddRange(collection);
}
public bool Contains(Employee item)
{
return this.List.Contains(item);
}
public void Insert(int index, Employee item)
{
this.List.Insert(index, item);
}
}
Now, we will add custom enumeration feature to iterate our custom collection. We will provide 2 enumeration, one will iterate into employees collection
and return employees with age > 50 called SeniorEmployeeEnumerator and the other will return employees with age < 50 called
JuniorEmployeeEnumerator.
public class SeniorEmployeeEnumerator : IEnumerable
{
EmployeeCollection list = new EmployeeCollection();
public SeniorEmployeeEnumerator(EmployeeCollection
emplist)
{
list = emplist;
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Age > 50)
{
yield return list[i];
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (GetEnumerator());
}
}
public class JuniorEmployeeEnumerator : IEnumerable
{
EmployeeCollection list = new EmployeeCollection();
public JuniorEmployeeEnumerator(EmployeeCollection
emplist)
{
list = emplist;
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Age < 50)
{
yield return list[i];
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (GetEnumerator());
}
}
So our EmployeeCollection will be like,
public class EmployeeCollection : System.Collections.CollectionBase
{
// CollectionBase, IList, ICollection member
implementations
//Custom Enumerator
public SeniorEmployeeEnumerator
GetSeniorEmployeeEnumerator()
{
SeniorEmployeeEnumerator enume = new
SeniorEmployeeEnumerator(this);
return enume;
}
public JuniorEmployeeEnumerator
GetJuniorEmployeeEnumerator()
{
JuniorEmployeeEnumerator enume = new
JuniorEmployeeEnumerator(this);
return enume;
}
}
Usage:
Creating EmployeeCollection and binding to a grid,
EmployeeCollection emplist = new EmployeeCollection();
Employee emp1 = new
Employee ();
emp1.Name = "Fatima";
emp1.Age = 23;
emplist.Add(emp1);
Employee emp2 = new
Employee ();
emp2.Name = "Evangeline";
emp2.Age = 59;
emplist.Add(emp2);
Employee emp3 = new
Employee ();
emp3.Name = "Damien";
emp3.Age = 27;
emplist.Add(emp3);
Employee emp4 = new
Employee ();
emp4.Name = "Cameroon";
emp4.Age = 65;
emplist.Add(emp4);
Employee emp5 = new
Employee ();
emp5.Name = "Babu";
emp5.Age = 20;
emplist.Add(emp5);
GridView1.DataSource = emplist;
GridView1.DataBind();
|