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