T
Tony Johansson
Hi!
The complete source code is included.
Here in main I first fill a ListDictionary with some items then I
instansiate a new object of type
MyCollection which is derived from NameObjectCollectionBase.
In the C-tor of MyCollection I add all the items in the ListDirectory to the
baseclass using
this.BaseAdd((String)de.Key, de.Value );
Then in main a pass the object of type MyCollection to a static method
called PrintKeysAndValues
where I want to use foreach to write both the key and the value.
In the original code they use an indexer in this way
public DictionaryEntry this[ int index ]
{
get
{
return ( new DictionaryEntry( this.BaseGetKey(index),
this.BaseGet(index) ) );
}
}
which is called from PrintKeysAndValues and works as expected.
I can use the original way to write key and value and be happy with that but
I want to understand why I can't use the foreach because the baseclass
NameObjectCollectionBase do supply one which I have hopped to use in my
derived class MyCollection but it doesnt't work because the element that I
think is a DictioneryEntry is string so that give runtime error.
I have also tried to use this way but this will only write the value. I have
hopped that this current here would have been
a DictionaryEntry but it's a string.
IEnumerator e = myCol.GetEnumerator();
while (e.MoveNext())
Console.WriteLine(e.Current);
So can somebody explain why it's not possible to a foreach when the
baseclass have one that that derived class can inherit and use ??
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
public class SamplesNameObjectCollectionBase
{
public static void Main()
{
IDictionary d = new ListDictionary();
d.Add("red", "apple");
d.Add("yellow", "banana");
d.Add("green", "pear");
MyCollection myROCol = new MyCollection(d, true);
PrintKeysAndValues(myROCol);
}
public static void PrintKeysAndValues(MyCollection myCol)
{
IEnumerator e = myCol.GetEnumerator();
while (e.MoveNext())
Console.WriteLine(e.Current);
foreach (DictionaryEntry item in myCol)
{
Console.WriteLine("Key={0}, Value = {1}", item.Key, item.Value);
}
//This code work
//for (int i = 0; i < myCol.Count; i++)
//{
// Console.WriteLine("[{0}] : {1}, {2}", i, myCol.Key,
myCol.Value);
//}
}
}
public class MyCollection : NameObjectCollectionBase
{
public MyCollection() {}
public MyCollection( IDictionary d, Boolean bReadOnly )
{
foreach ( DictionaryEntry de in d )
{
this.BaseAdd((String)de.Key, de.Value );
}
this.IsReadOnly = bReadOnly;
}
}
public DictionaryEntry this[ int index ]
{
get { return ( new DictionaryEntry( this.BaseGetKey(index),
this.BaseGet(index) ) ); }
}
}
//Tony
The complete source code is included.
Here in main I first fill a ListDictionary with some items then I
instansiate a new object of type
MyCollection which is derived from NameObjectCollectionBase.
In the C-tor of MyCollection I add all the items in the ListDirectory to the
baseclass using
this.BaseAdd((String)de.Key, de.Value );
Then in main a pass the object of type MyCollection to a static method
called PrintKeysAndValues
where I want to use foreach to write both the key and the value.
In the original code they use an indexer in this way
public DictionaryEntry this[ int index ]
{
get
{
return ( new DictionaryEntry( this.BaseGetKey(index),
this.BaseGet(index) ) );
}
}
which is called from PrintKeysAndValues and works as expected.
I can use the original way to write key and value and be happy with that but
I want to understand why I can't use the foreach because the baseclass
NameObjectCollectionBase do supply one which I have hopped to use in my
derived class MyCollection but it doesnt't work because the element that I
think is a DictioneryEntry is string so that give runtime error.
I have also tried to use this way but this will only write the value. I have
hopped that this current here would have been
a DictionaryEntry but it's a string.
IEnumerator e = myCol.GetEnumerator();
while (e.MoveNext())
Console.WriteLine(e.Current);
So can somebody explain why it's not possible to a foreach when the
baseclass have one that that derived class can inherit and use ??
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
public class SamplesNameObjectCollectionBase
{
public static void Main()
{
IDictionary d = new ListDictionary();
d.Add("red", "apple");
d.Add("yellow", "banana");
d.Add("green", "pear");
MyCollection myROCol = new MyCollection(d, true);
PrintKeysAndValues(myROCol);
}
public static void PrintKeysAndValues(MyCollection myCol)
{
IEnumerator e = myCol.GetEnumerator();
while (e.MoveNext())
Console.WriteLine(e.Current);
foreach (DictionaryEntry item in myCol)
{
Console.WriteLine("Key={0}, Value = {1}", item.Key, item.Value);
}
//This code work
//for (int i = 0; i < myCol.Count; i++)
//{
// Console.WriteLine("[{0}] : {1}, {2}", i, myCol.Key,
myCol.Value);
//}
}
}
public class MyCollection : NameObjectCollectionBase
{
public MyCollection() {}
public MyCollection( IDictionary d, Boolean bReadOnly )
{
foreach ( DictionaryEntry de in d )
{
this.BaseAdd((String)de.Key, de.Value );
}
this.IsReadOnly = bReadOnly;
}
}
public DictionaryEntry this[ int index ]
{
get { return ( new DictionaryEntry( this.BaseGetKey(index),
this.BaseGet(index) ) ); }
}
}
//Tony