Define indexer
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace Indexers
{
// a simplified ListBox control
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)
{
if (ctr >= strings.Length)
{
// handle bad index
}
else
strings[ctr++] = theString;
}
// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
// add only through the add method
if (index >= ctr )
{
// handle error
}
else
strings[index] = value;
}
}
// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
}
public class TesterListBoxTest
{
[STAThread]
static void Main()
{
// create a new listbox and initialize
ListBoxTest lbt =
new ListBoxTest("Hello", "World");
Console.WriteLine("After creation...");
for (int i = 0;i<lbt.GetNumEntries();i++)
{
Console.WriteLine("lbt[{0}]: {1}",i,lbt[i]);
}
// add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("John");
lbt.Add("Galt");
Console.WriteLine("\nAfter adding strings...");
for (int i = 0;i<lbt.GetNumEntries();i++)
{
Console.WriteLine("lbt[{0}]: {1}",i,lbt[i]);
}
// test the access
string subst = "Universe";
lbt[1] = subst;
// access all the strings
Console.WriteLine("\nAfter editing strings...");
for (int i = 0;i<lbt.GetNumEntries();i++)
{
Console.WriteLine("lbt[{0}]: {1}",i,lbt[i]);
}
}
}
}
Related examples in the same category