Traversing a Collection

J

James

Hi.

I have created several Collection objects which inherit from

System.Collections.CollectionBase

but up until now I've been able to process them according to the order
I populate them from a database.

Now I want to be able to traverse backwards and forward through items
in my collection and apart from loading the whole collection into an
array or Arraylist, I can't work out how.

eg. I have items like "Book" and a collection like "Books".

I can easily process the items with
For each book as Book in m_Books
' do stuff
Next

But I need more flexibility and the collection must have some kind of
index inherited from the CollectionBase

Maybe the arraylist idea is the best but I'm hoping that someone can
tell me about something more elegant that I must have at my fingertips
?

James.
 
C

Cor Ligthert [MVP]

James,

In Net is a complete main namespace for Database handling, that is
System.Data. (ADONET) In that are a lot of classes for the handling in
memory. A major part of those are than again collection classes.

There is probably a lot of time taken in that, it is a major part. Why would
you do it on your own and ask us to help you with that?

Cor
 
G

Guest

James,

Your custom collection class should implement a strongly-typed Item property
that returns an item from the collection using an index.

Here is an example:

Default Public Property Item(ByVal Index As Integer) As clsStudent
Get
Return CType(Me.List.Item(Index), clsStudent)
End Get
Set(ByVal Value As clsStudent)
Me.List.Item(Index) = Value
End Set
End Property

Kerry Moorman
 
J

James

Yes!!. Thanks Kerry, that worked well (and very simple which is good).

I think that property will have to be a standard entry in every
collection class I can lay my hands on :)

Thanks again.
 

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