CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Custom Collection in C# - Part 2

By Satheesh babu
Posted On Feb 19,2008
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category:
Print this article.

Custom Collection in C# - Part 2

  Generic Custom Collection with C#

 

The Part1 of this article helped us to create a strongly typed custom collection by implementing CollectionBase class. In this article we will implement a generic custom collection by inheriting the same CollectionBase abstract class and add sorting feature to the collection object.

 

The implementation of Generic custom collection will be,

public bool public class Collection<T>:CollectionBase

{

       public Collection()

       {

              //

              // TODO: Add constructor logic here

              //

       }

    public T this[int index]

    {

        get { return (T)this.List[index]; }

        set { this.List[index] = value; }

    }

 

    public int IndexOf(T item)

    {

        return this.List.IndexOf(item);

    }

 

    public int Add(T item)

    {

        return this.List.Add(item);

    }

 

    public void Remove(T item)

    {

        this.List.Remove(item);

    }

 

    public void CopyTo(Array array, int index)

    {

        this.List.CopyTo(array, index);

    }

 

    public void AddRange(Collection<T> collection)

    {

        for (int i = 0; i < collection.Count; i++)

        {

            this.List.Add(collection[i]);

        }     

    }

    public void AddRange(T[] collection)

    {

        this.AddRange(collection);     

    }

Contains(T item)

    {

        return this.List.Contains(item);

    }

    public void Insert(int index, T item)

    {

        this.List.Insert(index, item);

    }

}

Now, we will add a sort method to this generic collection object. I will declare an enumeration called SortOrder to indicate the sort direction. We will use IComparer interface to implement sort for this collection object.

 

public enum SortOrder

{

    ASC,

    DSC

}

class GenericComparer : IComparer

{

    private string _property;

    private SortOrder _order;

    public GenericComparer(string Property,SortOrder Order)

    {

       this._property = Property;

       this._order = Order;

    }

 

    //Sort

    public int Compare(object obj1, object obj2)

    {

        int returnValue;

        Type type = obj1.GetType();

        PropertyInfo propertie1 = type.GetProperty(_property);

 

        Type type2 = obj2.GetType();

        PropertyInfo propertie2 = type2.GetProperty(_property);

 

        object finalObj1 = propertie1.GetValue(obj1, null);

        object finalObj2 = propertie2.GetValue(obj2, null);

 

        IComparable Ic1 = finalObj1 as IComparable;

        IComparable Ic2 = finalObj2 as IComparable;

 

        if (_order == SortOrder.ASC)

        {

            returnValue = Ic1.CompareTo(Ic2);

        }

        else

        {

            returnValue = Ic2.CompareTo(Ic1);

        }

        return returnValue;

    }

}

 




So the final generic Collection will be like,

 

public class Collection<T>:CollectionBase

{

 

 

// CollectionBase, IList, ICollection member implementations

 

 

  // Sort

    public void Sort(string SortExpression,SortOrder Order)

    {

        GenericComparer comp = new GenericComparer(SortExpression, Order);

        this.InnerList.Sort(comp);

    } 

}

 

Usage:

Consider the Employee BO,

 

public class Employee

{

       public Employee()

       {

              //

              // TODO: Add constructor logic here

              //

       }

    private int _Age;

    private string _Name;

 

    public int Age

    {

        get { return _Age; }

        set { _Age = value; }

    }

 

    public string Name

    {

        get { return _Name; }

        set { _Name = value; }

    }

 

}

 

Creating  a Collection of Employee BO and binding to a grid,

 

        Collection<Employee> emplist = new Collection<Employee>();

      

        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();

Sorting the collection

 

emplist.Sort("Age",SortOrder.DSC);

 

        foreach (Employee em in emplist)

        {

            Response.Write(em.Name +" "+em.Age +"<br>");

 

        }

 

Download source:

Download Source
Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments