By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,558 Members | 1,673 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,558 IT Pros & Developers. It's quick & easy.

Strongly Typed Key Value Collections within a For Next loop

P: n/a
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.
Feb 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

The code is in c# but I think this article will help.
http://www.codeproject.com/csharp/hashlistarticle.asp

Ken
--------------------
"Simon" <s@v.c> wrote in message
news:43**********************@ptn-nntp-reader02.plus.net...
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.

Feb 8 '06 #2

P: n/a
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.

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OP**************@TK2MSFTNGP09.phx.gbl...
Hi,

The code is in c# but I think this article will help.
http://www.codeproject.com/csharp/hashlistarticle.asp

Ken
--------------------
"Simon" <s@v.c> wrote in message
news:43**********************@ptn-nntp-reader02.plus.net...
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.


Feb 8 '06 #3

P: n/a
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
"Simon" <s@v.c> wrote in message
news:43**********************@ptn-nntp-reader02.plus.net...
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.

Feb 8 '06 #4

P: n/a
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.

"Jim Wooley" <jw******@bellsouth.net> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
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
"Simon" <s@v.c> wrote in message
news:43**********************@ptn-nntp-reader02.plus.net...
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.


Feb 9 '06 #5

P: 1
Hi all,

Has anybody solved Simon's problem? (see below)
I am having exactly the same problem...

Many thanks,

Jean-Paul


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.
May 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.