using System;
public class Accounts{
private PersonAccountPair[] store;
private int next;
private const int LIMIT = 100;
public Accounts(){
store = new PersonAccountPair[LIMIT];
next = 0;
}
public BankAccount this[Person p]{
get {
int i = IndexOfPerson(p);
return store[i].Account;
}
set {
int i = IndexOfPerson(p);
if (i < next)
store[i].Account = value;
else {
store[next] = new PersonAccountPair(p, value);
next++;
}
}
} // End indexer
private int IndexOfPerson(Person p){
for(int i = 0; i < next; i++){
if (store[i].Person == p)
return i;
}
return next;
}
} | |
An indexer in class Accounts.
The index type is Person.
Thus, given a Person object,
we can get/set the persons
bank account.
|