Inheriting from CollectionBase and using For..Each

K

Kyle Novak

I have a question about strongly typed objects when looping through a
collection based on the CollectionBase object and using a For..Each loop.

I have 2 objects:

-Invoice: Holds all properties related to an invoice
-InvoiceCollection: Inherited from Collectionbase class and holds Invoice
objects

The InvoiceCollection class is as follows:

'***********************************************************************
'* InvoiceCollection Class
'***********************************************************************
Public Class InvoiceCollection
Inherits CollectionBase

Private mDS As DataSet

Public Function GetAllInvoices()
list.Clear() 'Clear any previous objects

'Retrieve all the invoices
Dim oDL As New InvoiceDL.InvoiceDL()
mDS = oDL.GetAllInvoices()

'Create the invoice objects and add to the collection
Dim oDR As DataRow
For Each oDR In mDS.Tables(0).Rows
Dim oInvoice As New Invoice(Me, oDR)
list.Add(oInvoice)
Next
End Function

Public ReadOnly Property Item(ByVal Index As Integer) As Invoice
Get
Return CType(list.Item(Index), Invoice)
End Get
End Property
End Class

Now in my client, I create an instance of the InvoiceCollection class, call
the GetAllInvoices function, and loop through the collection.

'***********************************************************************
'* Client Code
'***********************************************************************
Dim oIBL As New InvoiceCollection()
oIBL.GetAllInvoices()

Dim oInvoice As Invoice
For Each oInvoice In oIBL

'Do something in here
Next

Now, my question is in the For Each loop, the " in oIBL" part. Is it
returning an object of type Invoice or object of type Object? I'm worried,
because of my lack of knowledge, that the "in oIBL" part is returning a type
of Object. I don't want the For..Each loop doing an implicit conversion
from type Object to type Invoice. What I want is a type Invoice always
returned.

TIA,

Kyle Novak
 
K

Ken Tucker [MVP]

Hi,

Here is an example of an custom collection i have been working on.
That you can use with for each loop.

Article.
http://msdn.microsoft.com/library/d.../html/vaconcreatingyourowncollectionclass.asp

Some sample code.


Public Class NewsGroupMessageCollection

Inherits System.Collections.CollectionBase



Public Sub Add(ByVal value As NNTP.NewsGroupMessage)

list.Add(value)

End Sub

Public Sub Remove(ByVal Index As Integer)

If Index > Count - 1 Or Index < 0 Then

Throw New Exception("Invalid Index")

Else

list.RemoveAt(Index)

End If

End Sub

Public ReadOnly Property Item(ByVal Index As Integer) As
NNTP.NewsGroupMessage

Get

Return CType(list.Item(Index), NNTP.NewsGroupMessage)

End Get

End Property



End Class



Ken

---------------------------------

I have a question about strongly typed objects when looping through a
collection based on the CollectionBase object and using a For..Each loop.

I have 2 objects:

-Invoice: Holds all properties related to an invoice
-InvoiceCollection: Inherited from Collectionbase class and holds Invoice
objects

The InvoiceCollection class is as follows:

'***********************************************************************
'* InvoiceCollection Class
'***********************************************************************
Public Class InvoiceCollection
Inherits CollectionBase

Private mDS As DataSet

Public Function GetAllInvoices()
list.Clear() 'Clear any previous objects

'Retrieve all the invoices
Dim oDL As New InvoiceDL.InvoiceDL()
mDS = oDL.GetAllInvoices()

'Create the invoice objects and add to the collection
Dim oDR As DataRow
For Each oDR In mDS.Tables(0).Rows
Dim oInvoice As New Invoice(Me, oDR)
list.Add(oInvoice)
Next
End Function

Public ReadOnly Property Item(ByVal Index As Integer) As Invoice
Get
Return CType(list.Item(Index), Invoice)
End Get
End Property
End Class

Now in my client, I create an instance of the InvoiceCollection class, call
the GetAllInvoices function, and loop through the collection.

'***********************************************************************
'* Client Code
'***********************************************************************
Dim oIBL As New InvoiceCollection()
oIBL.GetAllInvoices()

Dim oInvoice As Invoice
For Each oInvoice In oIBL

'Do something in here
Next

Now, my question is in the For Each loop, the " in oIBL" part. Is it
returning an object of type Invoice or object of type Object? I'm worried,
because of my lack of knowledge, that the "in oIBL" part is returning a type
of Object. I don't want the For..Each loop doing an implicit conversion
from type Object to type Invoice. What I want is a type Invoice always
returned.

TIA,

Kyle Novak
 

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