Define indexer : Indexer « Class Interface « C# / C Sharp






Define indexer

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

1.indexed properties
2.Indexer with complex logic
3.Use an indexer to create a fail-soft arrayUse an indexer to create a fail-soft array
4.Overload the FailSoftArray indexerOverload the FailSoftArray indexer
5.Indexers don't have to operate on actual arraysIndexers don't have to operate on actual arrays
6.Two dimensional indexer
7.A two-dimensional fail-soft arrayA two-dimensional fail-soft array
8.Create a specifiable range array classCreate a specifiable range array class
9.Indexer: allow array like indexIndexer: allow array like index
10.illustrates the use of an indexer 1illustrates the use of an indexer 1
11.Implements an indexer in a classImplements an indexer in a class
12.Illustrates the use of an indexer
13.Implements an indexer and demonstrates that an indexer does not have to operate on an arrayImplements an indexer and demonstrates that an indexer does not have to operate on an array
14.C# Properties and Indexers
15.Indexing with an Integer IndexIndexing with an Integer Index
16.Indexing with an String Index
17.Indexing with Multiple ParametersIndexing with Multiple Parameters
18.Return class object from indexer