G'day,
In the simple code example below I am using a simple List to store multiple Employee objects within an Employees object. I would like to change the List to a SortedList with the Employee.Number used as the TKey, so that the foreach loop will always print the employees in order of ascending Employee.Number and so that the Item method in the Employees class will retrieve an Employee by its Employee.Number rather than by its index position in the List.
I thought this would be trivial, but I don't seem to be able to syntax right to make the SortedList iterable from outside the Employees class...? So I would very much appreciate it if somebody could modify my example code to show me the way.
Thanking you for your assistance,
Martin.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
clsEmployees Employees = new clsEmployees();
Employees.Add(new clsEmployee(3, "Fred"));
Employees.Add(new clsEmployee(5, "Jane"));
Employees.Add(new clsEmployee(2, "Bob"));
Employees.Add(new clsEmployee(1, "Sarah"));
foreach (clsEmployee Employee in Employees)
{
Console.WriteLine("{0} {1}", Employee.Number, Employee.Name);
}
Console.ReadLine();
}
}
public class clsEmployee
{
public int Number;
public string Name;
public clsEmployee(int SetNumber, string SetName)
{
Number = SetNumber;
Name = SetName;
}
}
public class clsEmployees : IEnumerable<clsEmployee>
{
private List<clsEmployee> EmployeeList;
public clsEmployees() { EmployeeList = new List<clsEmployee>(); }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public IEnumerator<clsEmployee> GetEnumerator() { return EmployeeList.GetEnumerator(); }
public void Add(clsEmployee NewEmployee) { EmployeeList.Add(NewEmployee); }
public clsEmployee Item(int Index) { return EmployeeList[Index]; }
}
}
It is not clear from your question what specifically you're having trouble with. You don't show any code that attempts to use SortedList<TKey, TValue>
, so there's no way to know exactly what you're doing wrong.
Here is how I might approach it:
public class clsEmployees : IEnumerable<clsEmployee>
{
private SortedList<int, clsEmployee> EmployeeList;
public clsEmployees()
{
EmployeeList = new SortedList<int, clsEmployee>();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<clsEmployee> GetEnumerator()
{
return EmployeeList.Values.GetEnumerator();
}
public void Add(clsEmployee NewEmployee)
{
EmployeeList.Add(NewEmployee.Number, NewEmployee);
}
public clsEmployee Item(int Index)
{
return EmployeeList[Index];
}
}
Notes:
SortedList<TKey, TValue>
type requires two type parameters. The first is the key type and the second is the value type. Your key is the Number
property and so has to be type int
, while the value is the clsEmployee
object itself and so has to be that type.Values
property's GetEnumerator()
result.NewEmployee.Number
SortedList<TKey, TValue>
type's indexer works just like you want, so no need to change anything there. :)This looks a lot like homework. If it's part of an assignment, you should probably still review your uncertainty and difficulty with the teacher, so that they can make sure you've learned the concepts that they want you to learn.