Returns a String array that contains all the keys in NameObjectCollectionBase : NameObjectCollectionBase « Collections Data Structure « C# / C Sharp






Returns a String array that contains all the keys in NameObjectCollectionBase

 

using System;
using System.Collections;
using System.Collections.Specialized;

public class MyCollection : NameObjectCollectionBase  {

   private DictionaryEntry _de = new DictionaryEntry();
   public DictionaryEntry this[ int index ]  {
      get  {
         _de.Key = this.BaseGetKey( index );
         _de.Value = this.BaseGet( index );
         return( _de );
      }
   }
   public MyCollection( IDictionary d )  {
      foreach ( DictionaryEntry de in d )  {
         this.BaseAdd( (String) de.Key, de.Value );
      }
   }
   public String[] AllKeys  {
      get  {
         return( this.BaseGetAllKeys() );
      }
   }
   public Array AllValues  {
      get  {
         return( this.BaseGetAllValues() );
      }
   }
   public String[] AllStringValues  {
      get  {
         return( (String[]) this.BaseGetAllValues( typeof(System.String) ) );
      }
   }

}

public class SamplesNameObjectCollectionBase  {
   public static void Main()  {
      IDictionary d = new ListDictionary();
      d.Add( "A", "a" );
      d.Add( "B", "b" );
      d.Add( "C", "c" );
      MyCollection myCol = new MyCollection( d );
      Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count );
      PrintKeysAndValues( myCol );

      foreach ( String s in myCol.AllKeys )  {
         Console.WriteLine( "   {0}", s );
      }
      foreach ( Object o in myCol.AllValues )  {
         Console.WriteLine( "   {0}", o.ToString() );
      }
      foreach ( String s in myCol.AllValues )  {
         Console.WriteLine( "   {0}", s );
      }
   }
   public static void PrintKeysAndValues( MyCollection myCol )  {
      for ( int i = 0; i < myCol.Count; i++ )  {
         Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value );
      }
   }
}

   
  








Related examples in the same category

1.NameObjectCollectionBase Demo
2.NameObjectCollectionBase is the base class for a collection of associated String keys and Object values
3.Adds an entry with the specified key and value into NameObjectCollectionBase
4.Removes all entries from NameObjectCollectionBase
5.Gets the value of the entry at the specified index of NameObjectCollectionBase
6.Whether NameObjectCollectionBase contains entries whose keys are not null.
7.Removes the entries with the specified key from NameObjectCollectionBase
8.Sets the value of the entry at the specified index of NameObjectCollectionBase
9.Whether NameObjectCollectionBase is read-only.
10.Represents a collection of associated String keys and String values