471,071 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

Inheriting from CollectionBase and using For..Each

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
Nov 21 '05 #1
1 2727
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/de...ctionclass.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

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

"Kyle Novak" <kn****@nowhere.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by m. pollack | last post: by
11 posts views Thread by Noah Coad [MVP .NET/C#] | last post: by
1 post views Thread by Dave Veeneman | last post: by
4 posts views Thread by Siu | last post: by
1 post views Thread by Matthew Roberts | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.