Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    The class Accounts - the associative array class.Lecture 5 - slide 15 : 29
Program 4
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.