Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Sets the value of the entry at the specified index of NameObjectCollectionBase

using System; using System.Collections; using System.Collections.Specialized; public class MyCollection : NameObjectCollectionBase  {    public Object this[ int index ]  {       get  {          return( this.BaseGet( index ) );       }       set  {          this.BaseSet( index, value );       }    }    public Object this[ String key ]  {       get  {          return( this.BaseGet( key ) );       }       set  {          this.BaseSet( key, value );       }    }    public String[] AllKeys  {       get  {          return( this.BaseGetAllKeys() );       }    }    public MyCollection( IDictionary d )  {       foreach ( DictionaryEntry de in d )  {          this.BaseAdd( (String) de.Key, de.Value );       }    } } 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 );       PrintKeysAndValues2( myCol );       Console.WriteLine();       myCol[1] = "sunflower";       PrintKeysAndValues2( myCol );       Console.WriteLine();       myCol["red"] = "tulip";       PrintKeysAndValues2( myCol );    }    public static void PrintKeysAndValues2( MyCollection myCol )  {       foreach ( String s in myCol.AllKeys )  {          Console.WriteLine( "{0}, {1}", s, myCol[s] );       }    } }