在程序开发过程中,常常需要对一组对象进行访问,通常是创建数组列表,通过操作数组的方式进行访问。
C#提供的索引指示器使我们可以方便且高效的完成对一组对象的访问。通常,我们先创建一个容器类,用于
存储对象,并且通过实现枚举器接口提供相应的操作方法。以下示例程序演示了如何创建并使用索引指示器。
第一步:创建容器类
这段代码中,使用了ARRAYLIST,使我们可以利用ARRAYLIST的功能特性管理对象;另外,实现IENUMERATOR接口,提供如MOVENEXT,RESET等方法,并且使容器类可以支持FOREACH操作。
class Employees:IEnumerator //为了使容器支持(FOREACH...IN...)操作,必须实现IENUMERATOR接口)
{
private ArrayList m_Employees; //定义一个ARRAYLIST对象
private int m_MaxEmployees; //定义容器可接受的最大对象数量
//构造器,创建ARRAYLIST对象,并且定义可接受的最大对象数量
public Employees(int MaxEmployees)
{
m_MaxEmployees = MaxEmployees;
m_Employees = new ArrayList(MaxEmployees);
}
//按照索引ID创建索引指示器
public Employee this[int index]
{
get
{
if (index < 0 || index > m_Employees.Count -1)
{
return null;
}
return (Employee) m_Employees[index];
}
set
{
if (index <0 || index > m_MaxEmployees-1)
{
return ;
}
m_Employees.Insert(index,value);
}
}
//自定义索引指示器
public Employee this[string SSN]
{
get
{
Employee empReturned = null;
foreach (Employee employee in m_Employees)
{
if (employee.SSN == SSN)
{
empReturned = employee;
break;
}
}
return empReturned;
}
}
//提供容器内对象数量
public int Length
{
get
{
return m_Employees.Count;
}
}
//实现IENUMERATOR接口
public IEnumerator GetEnumerator()
{
return m_Employees.GetEnumerator();
}
public bool MoveNext(){return m_Employees.GetEnumerator().MoveNext();}
public void Reset(){m_Employees.GetEnumerator().Reset();}
public object Current
{
get
{
return m_Employees.GetEnumerator().Current;
}
}
}
第二步:构建对象
以下代码实现了一个类Employee
//构建对象
class Employee
{
private string m_firstname;
private string m_middlename;
private string m_lastname;
private string m_SSN;
//构造器,当实例化对象时对属性成员赋值
public Employee(string FirstName,string MiddleName,string LastName,string SSN)
{
m_firstname = FirstName;
m_middlename = MiddleName;
m_lastname = LastName;
m_SSN = SSN;
}
public string FirstName
{
get {return m_firstname;}
set {m_firstname = value;}
}
public string LastName
{
get{return m_lastname;}
set{m_lastname = value;}
}
public string MiddleName
{
get{return m_middlename;}
set{m_middlename = value;}
}
public string SSN
{
get{return m_SSN;}
set{m_SSN = value;}
}
}
第三步:使用索引指示器
创建一个程序,对Employee实例化,并且将对象加入到容器类(Employees)中;程序判断是否有控制台参数输入,如果有,
将根据参数查询容器中的对象,否则显示容器中所有的对象信息。
class IndexerSample
{
static void Main(string[] args)
{
try
{
//创建容器类对象
Employees employees = new Employees(4);
string ssn = "";
//将实例化的EMPLOYEE对象加入到容器类对象EMPLOYEES中
employees[0] = new Employee("Timothy","Arthur","Tucker","555-555-555");
employees[1] = new Employee("Jackie","zxh","Cheung","555-555-552");
employees[2] = new Employee("John","JHK","Kong","555-555-553");
employees[3] = new Employee("Ken","KNC","Chang","555-555-551");
if (args.Length > 0)
{
foreach(string s in args)
{
ssn = ssn + s;
}
//根据自定义的索引关键字SSN查找对象
Employee employee = employees[ ssn.ToString() ];
if (employee !=null )
{
string name = employee.FirstName + " " + employee.LastName;
Console.WriteLine("Name: {0},SSN:{1}", name,ssn);
}
else
{
Console.WriteLine("Can Not find the record !");
}
}
else
{ //显示容器中所有的对象信息
for (int i = 0 ; i < employees.Length; i++)
{
string name = employees[i].FirstName + " " +
employees[i].MiddleName + " " +
employees[i].LastName;
ssn = employees[i].SSN;
Console.WriteLine("Name: {0},SSN:{1}", name,ssn);
}
}
}
catch (Exception e)
{
Console.WriteLine (e.Message );
}
}
}