C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Enumeration & sorting of objects in C#


Posted Date: 01 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: Lalitha MaheswaranMember Level: Bronze    
Rating: 1 out of 5Points: 10




using System;
using System.Collections;

//Class Employee
class Employee
{
public string EmpName;
public string Dept;

public Employee(string szName, string szDept)
{
EmpName = szName;
Dept = szDept;
}
}

//Declare the class to be implement IEnumerable
//Employees class contains the Employees list and enumeration will result in going thru employees
class Employees
{

//Declare a member of Employees
public ArrayList oEmployees;

//Constructor for Employees
public Employees ()
{
oEmployees = new ArrayList();
}

//Declare a enumerator member
class EnumEmployee:IEnumerator
{
//Declare a variable to hold current index
//Declare a variable of Enumerable class that is Employee
public int CurrentIndex;
public Employees oEmpList;

//Constructor
public EnumEmployee(Employees oEmps)
{
oEmpList = oEmps;
CurrentIndex = -1;
}

//Declare a method to get the current employee
// Mandatorily implemented...Dont change the name
public object Current
{
get
{

return oEmpList.oEmployees[CurrentIndex];

}
}

//Handle movenext : Mandatorily implemented...Dont change the name
public bool MoveNext()
{
if (CurrentIndex < oEmpList.oEmployees.Count - 1)
{
CurrentIndex++;
return true;
}
else
return false;
}

//Reset the variable : Mandatorily implemented...Dont change the name
public void Reset()
{
CurrentIndex = -1;
}
}

//Class for Comparison
//Member for sort order
public enum EmployeeSortOrder {eSortNameDesc, eSortNameAsc, eSortDeptDesc, eSortDeptAsc};
class EmployeesSort : IComparer
{
//Sort order
private int SortOrder;

//Constructor
public EmployeesSort(int Sort)
{
SortOrder = Sort;
}

//Implement Compare function
public int Compare(object oEmployee1, object oEmployee2)
{
switch (SortOrder)
{
case (int) EmployeeSortOrder.eSortNameDesc:
{
return ((Employee)(oEmployee2)).EmpName.CompareTo(((Employee)(oEmployee1)).EmpName);
}
case (int) EmployeeSortOrder.eSortNameAsc:
{
return ((Employee)(oEmployee1)).EmpName.CompareTo(((Employee)(oEmployee2)).EmpName);
}
case (int) EmployeeSortOrder.eSortDeptDesc:
{
return ((Employee)(oEmployee2)).Dept.CompareTo(((Employee)(oEmployee1)).Dept);
}
case (int) EmployeeSortOrder.eSortDeptAsc:
{
return ((Employee)(oEmployee1)).Dept.CompareTo(((Employee)(oEmployee2)).Dept);
}
default:
{
return ((Employee)(oEmployee1)).EmpName.CompareTo(((Employee)(oEmployee2)).EmpName);
}
}
}
}

//Write a function to get the enumerator
public IEnumerator GetEnumerator( )
{
return new EnumEmployee ( this );
}

//Write a function to call sort
public void SortEmployees (int SortType)
{
oEmployees.Sort(new EmployeesSort(SortType));
}

//Now.....use the enumeration :-))
public static void Main()
{
Employee oTemp;

//Create the Employees class
Employees oEmpList= new Employees();

//Add Employees to the oEmployees member
oEmpList.oEmployees.Add(new Employee("Lalitha", "SE"));
oEmpList.oEmployees.Add(new Employee("Lal", "Associate"));
oEmpList.oEmployees.Add(new Employee("KLA", "Executive"));

//Now....loop using foreach
Console.WriteLine("Using ForEach");
Console.WriteLine("----- -------");

foreach ( Employee oEmp in oEmpList.oEmployees )
Console.WriteLine ( oEmp.EmpName ) ;

//Sort the Employees list
oEmpList.SortEmployees(1);

//Alright....Try enumeration...
//Get the enumerator and start enumeration
IEnumerator e = oEmpList.GetEnumerator( ) ;

Console.WriteLine("\n\n Using Enumerator");
Console.WriteLine("----- ----------\n");
while ( e.MoveNext( ) )
{
oTemp = (Employee)(e.Current);
Console.WriteLine ( oTemp.EmpName + " " + oTemp.Dept) ;
oTemp = null;
}
}

}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: SortedList: collection that stores key/value pairs
Previous Resource: Migrating VB6.0 Application to VB.Net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use