associative-arrays/accounts.cs - 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; } }