在程序開發過程中,常常需要對一組對象進行訪問,通常是創建數組列表,通過操作數組的方式進行訪 問.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);
}
}
}