472,811 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 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 2840
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: m. pollack | last post by:
Hi all, I've been using the CollectionBase class to implement a strongly-typed collection, but I have noticed that the RemoveAt method does not seem to call the "On" hook methods (OnRemove,...
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
1
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
2
by: Scott Reynolds | last post by:
I am having a problem exposing a class inherited from the collection base class as a webservice. If I expose the collection on a web page all works well and I am very happy. However when I try and...
1
by: Dave Veeneman | last post by:
I have created a collection class derived from CollectionBase that can be nested several layers deep. so my root object has several child objects, each of which has several grandchild objects, and...
2
by: Mark Overstreet | last post by:
I am writing custom collections for my business objects but I need support for the foreach construct. I also need key support so I am inheriting from NameObjectCollectionBase. I need to support...
4
by: Siu | last post by:
Hi, I've written a class which inheritates from the CollectionBase class public class MyClass : CollectionBase { } An object istantiated from MyClass can be transformed in a DataSet? does...
1
by: Matthew Roberts | last post by:
Howdy Everyone, I am having trouble understanding the process of creating a type-safe collection by inheriting from the CollectionBase class. I have done it plenty of times, but now that I sit...
5
by: jc | last post by:
RE: Two Classes with the same Data Structure.. saving code? Inheriting a structure? I have two classes. One in Inherits System.Collections.CollectionBase, the other does not, but they both have...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.