GOA - System.Collections.Specialized.NameObjectCollectionBase

NameObjectCollectionBase Class


Provides the abstract base class for a collection of associated String keys and Object values that can be accessed either with the key or with the index.

Definition

public abstract class NameObjectCollectionBase: Object, ICollection, IEnumerable

Members Table

MethodDescription
BaseAddAdds an entry with the specified key and value into the NameObjectCollectionBase instance.
BaseClearRemoves all entries from the NameObjectCollectionBase instance.
BaseGetOverloaded. Gets the value of the specified entry of the NameObjectCollectionBase.
BaseGetAllKeysReturns a String array that contains all the keys in the NameObjectCollectionBase instance.
BaseGetAllValuesReturns an Object array that contains all the values in the NameObjectCollectionBase instance.
BaseGetKeyGets the key of the entry at the specified index of the NameObjectCollectionBase instance.
BaseHasKeysGets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.
BaseRemoveRemoves the entries with the specified key from the NameObjectCollectionBase instance.
BaseRemoveAtRemoves the entry at the specified index of the NameObjectCollectionBase instance.
BaseSetOverloaded. Sets the value of the specified entry of the NameObjectCollectionBase.
CopyToWhen implemented by a class, copies the elements of the ICollection into an Array, starting at a particular Array index. (Inherited from ICollection)
EqualsOverloaded. Compares two objects to determine if they are equal. (Inherited from Object)
GetEnumeratorReturns an enumerator that can iterate through the NameObjectCollectionBase.
GetHashCodeServes as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object)
GetTypeGets the Type of the current instance. (Inherited from Object)
NameObjectCollectionBaseOverloaded. Initializes a new instance of NameObjectCollectionBase.
ReferenceEqualsDetermines whether the specified Object instances are the same instance. (Inherited from Object)
System.Collections.ICollection.CopyToCopies the elements of the NameObjectCollectionBase to an Array, starting at a particular Array index.
ToStringReturns a String that represents the current Object. (Inherited from Object)
PropertyDescription
CountGets the number of key-and-value pairs contained in the NameObjectCollectionBase instance.
IsReadOnlyGets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.
IsSynchronizedWhen implemented by a class, gets a value indicating whether access to the ICollection is synchronized. (Inherited from ICollection)
KeysGets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.
SyncRootWhen implemented by a class, gets an object that can be used to synchronize access to the ICollection. (Inherited from ICollection)
System.Collections.ICollection.IsSynchronizedGets a value indicating whether access to the NameObjectCollectionBase is synchronized.
System.Collections.ICollection.SyncRootGets an object that can be used to synchronize access to the NameObjectCollectionBase.

Inheritance Hierarchy

Object
NameObjectCollectionBase
NameValueCollection

Examples

// This examples shows how to implement a NameObjectCollectionBase.

// It shows also some properties and method of the NameObjectCollectionBase class.

 

using System;

using System.Diagnostics;

using System.Collections;

using System.Collections.Specialized;

 

public class SampleNameObjectCollectionBase

{

     static void Main()

     {

          // Creates and initializes a new IDictionary.

          IDictionary d = new ListDictionary();

 

          d.Add( "red", "apple" );

          d.Add( "yellow", "banana" );

          d.Add( "orange ", "orange" );

 

          // Creates and initializes a new collection by adding the element of the IDictionary.

          TestCollection myROCol = new TestCollection( d, false);

          Debug.WriteLine("Initial contents of the collection=");

          DisplayValues(myROCol);

 

          // Adds elements to the collection.

          myROCol.Add("This", "Hello");

          myROCol.Add("is", "World");

          myROCol.Add("a", "!!");

          myROCol.Add("sample",null);

          myROCol.Add(null, "test");

          myROCol.Add(null, null);

 

          // Displays the contents of the collection.

          Debug.WriteLine("\nContents of the collection after adding some new elements=");

          DisplayValues(myROCol);

 

          // Displays of some of the properties of the collections.

          Debug.WriteLine("Properties of the collection=");

          bool b = myROCol.HasKeys;

          Debug.WriteLine("HashKeys="+b.ToString());

 

          // Sets some elements of the collection.

          myROCol.Set("test", null);

          myROCol.Set(0, "settings");

          myROCol.Set(2, 1.69);

 

          Debug.WriteLine("\nContents of the collection after setting to index 0=");

          DisplayValues2(myROCol);

 

          // Removes some elements of the collection.

          myROCol.Remove("red");

          myROCol.RemoveAt(2);

          Debug.WriteLine("\n"+@"Contents of the collection after removing the ""red"" element and those at index 2=");

          DisplayValues(myROCol);

 

          // Gets the elements using the Get method.

          object o = myROCol.Get("sample");

          if (o==null)

               Debug.WriteLine(@"Base Get of ""sample""= null");

          else

               Debug.WriteLine(@"Base Get of ""sample""=" +o.ToString());

 

          Debug.WriteLine("\nBase Get at index 2=" +myROCol[2]);

 

          string key0 = myROCol.GetKey(0);

          Debug.WriteLine("\nGet Key at index 0="+key0);

     }

 

     // Displays values using BaseGetAllKeys and BaseGetAllValues methods.

     public static void DisplayValues( TestCollection myCharColBase )

     {

          int count = myCharColBase.Count;

 

          object[] keys = new object[count];

          keys = myCharColBase.AllKeys;

 

          object[] values = new object[count];

          values = myCharColBase.AllValues;

 

          for(int y=1; y<=count;y++)

          {

               if (keys[y-1]==null&&values[y-1]==null)

               {

                    Debug.WriteLine("\tnull:null");

               }

               else

                    if (keys[y-1]==null)

                         Debug.WriteLine("\tnull:" +values[y-1]);

                    else

                         if (values[y-1]==null)

                         {

                              Debug.WriteLine("\t"+keys[y-1]+":null");

                         }

                         else

                              Debug.WriteLine("\t"+keys[y-1]+":"+values[y-1]);

          }

     }

 

     // Displays values using BaseGetAllKeys methods.

     public static void DisplayValues2( TestCollection myCol )

     {

          foreach ( String s in myCol.AllKeys )

          {

               if (s==null&&myCol[s]==null)

               {

                    Debug.WriteLine("\tnull:null");

               }

               else

                    if (s==null)

                         Debug.WriteLine("\tnull:" +myCol[s]);

                    else

                         if (myCol[s]==null)

                         {

                              Debug.WriteLine("\t"+s+":null");

                         }

                         else

                              Debug.WriteLine("\t"+s+":"+myCol[s] );

          }

     }

 

     // Displays values of an array.

     public static void DisplayArrayValues ( Array array )

     {

          for ( int i = 0; i < array.Length; i++ )

               if (array.GetValue(i)==null)

                    Debug.WriteLine("null ");

               else

                    Debug.Write(array.GetValue(i)+" ");

     }

}

 

public class TestCollection : NameObjectCollectionBase

{

     // Creates an empty collection.

     public TestCollection() {

     }

 

     // Adds elements from an IDictionary into the new collection.

     public TestCollection( IDictionary d, Boolean bReadOnly )

     {

          foreach ( DictionaryEntry de in d )

          {

               this.BaseAdd( (String) de.Key, de.Value );

          }

          this.IsReadOnly = bReadOnly;

     }

 

     // Gets a key-and-value pair (DictionaryEntry) using an index.

     public string this[ int index ]

     {

          get

          {

               string s;

               if (this.BaseGetKey(index)==null)

                    s = "null:"+this.BaseGet(index);

               else

                    s = this.BaseGetKey(index)+":"+this.BaseGet(index);

               return s;

          }

     }

 

     // Gets or sets the value associated with the specified key.

     public Object this[ String key ]

     {

          get

          {

               return( this.BaseGet( key ) );

          }

          set

          {

               this.BaseSet( key, value );

          }

     }

 

     // Gets a String array that contains all the keys in the collection.

     public String[] AllKeys

     {

          get

          {

               return( this.BaseGetAllKeys() );

          }

     }

 

     // Gets an Object array that contains all the values in the collection.

     public object[] AllValues

     {

          get

          {

               return( this.BaseGetAllValues() );

          }

     }

 

     // Gets a value indicating if the collection contains keys that are not null.

     public Boolean HasKeys

     {

          get

          {

               return( this.BaseHasKeys() );

          }

     }

 

     // Adds an entry to the collection.

     public void Add( String key, Object value )

     {

          this.BaseAdd( key, value );

     }

 

     // Removes an entry with the specified key from the collection.

     public void Remove( String key )

     {

          this.BaseRemove( key );

     }

 

     // Removes an entry in the specified index from the collection.

     public void RemoveAt( int index )

     {

          this.BaseRemoveAt( index );

     }

 

     // Clears all the elements in the collection.

     public void Clear()

     {

          this.BaseClear();

     }

 

     public Object Get( int index)

     {

          return (this.BaseGet( index ));

     }

 

     public Object Get(string name)

     {

          return (this.BaseGet( name ));

     }

 

     public string GetKey( int index )

     {

          return ( this.BaseGetKey(index));

     }

 

     public void Set (string name, Object value)

     {

          this.BaseSet(name, value);

     }

 

     public void Set (int index, Object value)

     {

          this.BaseSet(index, value);

     }

}

 

/*

 

The output of the sample.

 

Initial contents of the collection=

     red:apple

     yellow:banana

     orange :orange

 

Contents of the collection after adding some new elements=

     red:apple

     yellow:banana

     orange :orange

     This:Hello

     is:World

     a:!!

     sample:null

     null:test

     null:null

Properties of the collection=

     HashKeys=True

 

Contents of the collection after setting to index 0=

     red:settings

     yellow:banana

     orange :1.69

     This:Hello

     is:World

     a:!!

     sample:null

     null:test

     null:test

     test:null

 

Contents of the collection after removing the "red" element and those at index 2=

     yellow:banana

     orange :1.69

     is:World

     a:!!

     sample:null

     null:test

     null:null

     test:null

 

Base Get of "sample"= null

Base Get at index 2=is:World

Get Key at index 0=yellow

 

*/

Class Information

NamespaceSystem.Collections.Specialized
Flash LibrarySystem.scl
Flash Library Version2.0.0.1308
Silverlight LibrarySystem.Windows.Forms.dll



© 2003-2007 NETiKA Technologies. All rights reserved.