Strongly Typed Key Value Collections within a For Next loop

S

Simon

Hi all,

I am writing a windows application using vb.net 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)
intCollection.Remove(Attr.Name)
End Sub

Public Sub Remove(ByVal AttrName As String)
intCollection.Remove(AttrName)
End Sub

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

Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
Get
Return CType(intCollection.Item(AttrName), Attribute)
End Get
End Property
Default ReadOnly Property Item(ByVal index As Integer) As Attribute
Get
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
Next

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.
Regards,
Simon.
 
S

Simon

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.

Regards,
Simon.
 
J

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
Get
for each child as Attribute in List
if Child.ID=AttrName then
return Child
end if
Next
'Nothing found, NOTHING will be returned
End Get
End Property

Jim Wooley
 
S

Simon

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.

Regards,
Simon.
 

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

Top