/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace Indexers
{
// a simplified ListBox control
public class ListBoxTest
{
private string[] strings;
private int ctr = 0;
// initialize the listbox with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[256];
// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
// add a single string to the end of the listbox
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}
// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}
// helper method, given a string find
// first matching record that starts with the target
private int findString(string searchString)
{
for (int i = 0;i {
if (strings[i].StartsWith(searchString))
{
return i;
}
}
return -1;
}
// index on string
public string this[string index]
{
get
{
if (index.Length == 0)
{
// handle bad index
}
return this[findString(index)];
}
set
{
strings[findString(index)] = value;
}
}
// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
}
public class TesterIndexersList
{
public void Run()
{
// create a new listbox and initialize
ListBoxTest lbt =
new ListBoxTest("Hello", "World");
// add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("John");
lbt.Add("Galt");
// test the access
string subst = "Universe";
lbt[1] = subst;
lbt["Hel"] = "GoodBye";
// lbt["xyz"] = "oops";
// access all the strings
for (int i = 0; i {
Console.WriteLine("lbt[{0}]: {1}",i,lbt[i]);
}
}
[STAThread]
static void Main()
{
TesterIndexersList t = new TesterIndexersList();
t.Run();
}
}
}