Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each in DictionaryBase collections

Status
Not open for further replies.

jhall156

Programmer
Aug 27, 2001
711
0
0
US
I have created a dictionary base collection

Public Class SBTSessions

Inherits System.Collections.DictionaryBase

Public Sub Add(ByVal Session As SBTSession, ByVal Key As String)
Dictionary.Add(Key, Session)
End Sub

Default Public ReadOnly Property Item(ByVal Key As String) As SBTSession
Get
Return CType(Dictionary.Item(Key), SBTSession)
End Get
End Property

End Class

In my app I have created and populated an SBTSessions collection
then I declared a variable Session as SBTSession
and try to iterate through the collection with
For Each Session In MyCollection

and I get a runtime error that I'm attempting an invalid cast

What have I got wrong

BTW I have used an IEnumerator to loop through the collection but I'm anal and know I should be able to use For Each
For Each Session

JHall
 
You probably need to inherit from DictionaryBase, as it will always return objects of type Object, not what the contents of the collection are (this is why the generics in the next release of .NET will be so nice).

So, create a new class called SBTSessions that inherits from DictionaryBase, and then override the Add, Item, etc. properties/methods to return your data type -- SBTSession.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top