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.
public abstract class NameObjectCollectionBase: Object, ICollection, IEnumerable
ObjectNameObjectCollectionBase
// 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();
// 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++ )
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 );
}
}
// 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
{
}
}
// 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
{
}
}
// Adds an entry to the collection.
public void Add( String key, Object value )
{
}
// Removes an entry with the specified key from the collection.
public void Remove( String key )
{
}
// Removes an entry in the specified index from the collection.
public void RemoveAt( int index )
{
}
// Clears all the elements in the collection.
public void Clear()
{
}
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)
{
}
public void Set (int index, Object 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
*/
Namespace System.Collections.Specialized Flash Library System.scl Flash Library Version 2.0.0.1308 Silverlight Library System.Windows.Forms.dll
| © 2003-2007 NETiKA Technologies. All rights reserved. |