Strongly Typed Key Value Collections within a For Next loop



Hi all,

I am writing a windows application using on the 1.1 framework.

We have in the application, some strongly typed collections that have been
written as classes that do not inherit from collection base, but use an
internal collection, to hold the objects and then implement IEnumerator, see
example below,

Public Class AttributeCollection

Implements IEnumerable

Protected intCollection As New Collection

Public Overridable Sub Add(ByVal Attr As Attribute)
intCollection.Add(Attr, Attr.Name)
End Sub

Public Sub Remove(ByVal Attr As Attribute)
End Sub

Public Sub Remove(ByVal AttrName As String)
End Sub

Public Function Count() As Integer
Return intCollection.Count
End Function

Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
Return CType(intCollection.Item(AttrName), Attribute)
End Get
End Property
Default ReadOnly Property Item(ByVal index As Integer) As Attribute
Return CType(intCollection.Item(index), Attribute)
End Get
End Property

Public Function GetEnumerator() As System.Collections.IEnumerator
Implements System.Collections.IEnumerable.GetEnumerator
Return intCollection.GetEnumerator
End Function

End Class

These collection allow us to do a for each loop down the collection,

For each attr as Attribute in someAttrCollection
.... code

However, we are having problems with this type of collection so are moving
to inheriting our strongly typed collections from collectionbase. However
collectionbase does not use Key Value pairs. We have tried inheriting from
DictionaryBase, but that then stops all the for next loops from working as
the items it returns are dictionary entries, not the internal objects we
wish to get.

I know if we inherit from DictionaryBase we can change to a
For each attr as Attribute in someAttrCollection.Values

but there are 10's of thousands of lines of code, and we would prefer not to
have to change our for next loops if possible.

So the question, how do we create a strongly typed key value pair collection
that allows a for next down the created collection returning the objects
contained in the value part?

How the question is understood.

Thanks in advance for any help or advice.


Thanks for the reply Ken,

I have read this article, and it seems to be similar to what we already do
with our collections, by implementing interfaces on a base class, and
handling the objects in the collection internally, as I have shown in the
example, in my first post.

The current way we create our collections, similar to the article, is
working as we want in the windows app, however we are having problems with
these internal type collections in the ASP.NET app that uses the same
objects, which we cannot fathom, I have several posts in the ASP.NET
newsgroup regarding this.

We have converted a few of them to be implicitly inherited from
collectionbase, and the problems seem to have abated, although we think it's
too soon to say the problems are solved.

There are some of these collections that need to be key value type
collections, and they are used in loads of places, that we do not want to
change the code. So I was kind of hoping there would be a way to inherit
from one of the base collections, and retain the key value relationship, but
be able to simply For each down the collection, not the collection.values.


Jim Wooley

The guidance (for 1.1) is to inherit from CollectionBase. To do what you
need, you may just need to modify the Item property as follows:

Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
for each child as Attribute in List
if Child.ID=AttrName then
return Child
end if
'Nothing found, NOTHING will be returned
End Get
End Property

Jim Wooley


Hi Jim,

Thanks for taking the time to reply,

What you describe is not the problem, we can easily change the collections
to inherit from collectionbase, but that takes away the key value pairing,
which we require.

If we inherit from any of the key value type collection bases then the For
Next construct down the collection no longer works, and we have to change
all our for next loops to go down the values property within the collection
which we do not want to do.

So we are looking for a key Value type collection base, where the For Next
works on the collection itself, and for nexts down the values stored, not
the key value pairs.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question