M
Michael K. Walter
I'd like to create a strongly-typed collection of objects that are indexed
by a string value (key is a string). I'd also like to allow users to iterate
over the collection using a For-each loop where an object of the contained
type is returned (instead of just the standard DictionaryEntry object).
- only allow objects of type MyObj to be added to the collection
- return a MyObj type in the For-each loop
What's the best way to do this?
I tried to have my collection class inherit from DictionaryBase but I had
trouble getting it to return MyObj objects in the For-each. By default its
retruning DictionaryEntry objects and it wouldn't let me override the
inherited GetEnumerator() function.
I also looked at inheriting from CollectionBase but this takes an integer
for the key values.
Is creating a class that implements IEnumerable and contains a private
collection object the way to go here?
Thanks in advance for the help/advice.
Michael
by a string value (key is a string). I'd also like to allow users to iterate
over the collection using a For-each loop where an object of the contained
type is returned (instead of just the standard DictionaryEntry object).
- only allow objects of type MyObj to be added to the collection
- return a MyObj type in the For-each loop
What's the best way to do this?
I tried to have my collection class inherit from DictionaryBase but I had
trouble getting it to return MyObj objects in the For-each. By default its
retruning DictionaryEntry objects and it wouldn't let me override the
inherited GetEnumerator() function.
I also looked at inheriting from CollectionBase but this takes an integer
for the key values.
Is creating a class that implements IEnumerable and contains a private
collection object the way to go here?
Thanks in advance for the help/advice.
Michael