Indexer: allow array like index : Indexer « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » IndexerScreenshots 
Indexer: allow array like index
Indexer: allow array like index

/*
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 < || 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<strings.Length;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<lbt.GetNumEntries(); i++)
           {
               Console.WriteLine("lbt[{0}]: {1}",i,lbt[i]);
           }


       }

       [STAThread]
       static void Main()
       {
          TesterIndexersList t = new TesterIndexersList();
          t.Run();
       }
    }
 }

           
       
Related examples in the same category
1. Use an indexer to create a fail-soft arrayUse an indexer to create a fail-soft array
2. Overload the FailSoftArray indexerOverload the FailSoftArray indexer
3. Indexers don't have to operate on actual arraysIndexers don't have to operate on actual arrays
4. A two-dimensional fail-soft arrayA two-dimensional fail-soft array
5. Create a specifiable range array classCreate a specifiable range array class
6. Define indexerDefine indexer
7. illustrates the use of an indexer 1illustrates the use of an indexer 1
8. Implements an indexer in a classImplements an indexer in a class
9. Illustrates the use of an indexer
10. 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
11. C# Properties and Indexers
12. Indexing with an Integer IndexIndexing with an Integer Index
13. Indexing with an String Index
14. Indexing with Multiple ParametersIndexing with Multiple Parameters
w_w__w.__j_a___v__a__2__s__._c___o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.