⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-09-11

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 09-11: Making a ListBox an enumerable class

namespace Programming_CSharp
{
   using System;
   using System.Collections;
  
   // a simplified ListBox control
   public class ListBoxTest : IEnumerable
   {
      // private implementation of ListBoxEnumerator
      private class ListBoxEnumerator : IEnumerator
      {
         // public within the private implementation
         // thus, private within ListBoxTest
         public ListBoxEnumerator(ListBoxTest lbt)
         {
            this.lbt = lbt;
            index = -1;
         }

         // Increment the index and make sure the
         // value is valid
         public bool MoveNext()
         {
            index++;
            if (index >= lbt.strings.Length)
               return false;
            else
               return true;
         }

         public void Reset()
         {
            index = -1;
         }

         // Current property defined as the
         // last string added to the listbox
         public object Current
         {
            get
            {
               return(lbt[index]);
            }
         }

         private ListBoxTest lbt;
         private int index;
      }

      // Enumerable classes can return an enumerator
      public IEnumerator GetEnumerator()
      {
         return (IEnumerator) new ListBoxEnumerator(this);
      }
                                      
      // initialize the list box with strings
      public ListBoxTest(params string[] initialStrings)
      {
         // allocate space for the strings
         strings = new String[8]; 

         // 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 list box
      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;
         }
      }
   
      // publish how many strings you hold
      public int GetNumEntries()
      {
         return ctr;
      }

      private string[] strings;
      private int ctr = 0;
   }
    
   public class Tester
   {
      static void Main()
      {
         // create a new list box 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;

         // access all the strings
         foreach (string s in lbt)
         {
            Console.WriteLine("Value: {0}", s);
         }
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -