How to create a type-safe, collection class that supports For Each...Next?

A

Andrew J. Marshall

It was SO easy in VB6. Add a few methods, set a few funky properties and
*BAM*, type-safe collection class that supports the For Each...Next syntax.

I've been trying to do this in VB.NET with little success.

I started inherting DictionaryBase because of the Key retrieval ability, but
my code bombed on the For Each line because I had Option Strict On and the
types didn't match. I now loop with an Object and then use CType, but that's
BS! >8-(

Do I need to create my own Enumerator and overload Current()?

C'mon Generics!!!

Thanks in advance for help,
Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA
 
G

Guest

Try this..

(It is a collection of ContractSale objects). It will support the For...Each that you requir

Public Class ContractSale
Inherits CollectionBas

Public Function Add(ByVal strBranchNo As String, ByVal intTillNo As Integer,
ByVal strReceiptNo As String, ByVal strCompositeReceiptNo As String,
ByVal intContractID As Integer, ByVal strBarcode As String,
ByVal dteSaleDate As Date, ByVal strSurname As String,
ByVal strMobileNo As String, ByVal strIMEI As String,
ByVal strSIM As String,
ByVal strNetwork As String, ByVal strImageFileName As String) As ContractSal

Dim objNew As New ContractSal

With objNe
.BranchNo = strBranchN
.TillNo = intTillN
.ReceiptNo = strReceiptN
.CompositeReceiptNo = strCompositeReceiptN
.ContractID = intContractI
.Barcode = strBarcod
.SaleDate = dteSaleDat
.Surname = strSurnam
.MobileNo = strMobileN
.IMEI = strIME
.SIM = strSI
.Network = strNetwor
.ImageFileName = strImageFileNam
End Wit

list.Add(objNew
Add = objNe

End Functio
Public Sub Remove(ByVal intIndex As Integer
list.RemoveAt(intIndex
End Su

Public ReadOnly Property Item(ByVal intIndex As Integer) As ContractSal
Ge
Return CType(list.Item(intIndex), ContractSale
End Ge
End Propert

End Clas


----- Andrew J. Marshall wrote: ----

It was SO easy in VB6. Add a few methods, set a few funky properties an
*BAM*, type-safe collection class that supports the For Each...Next syntax

I've been trying to do this in VB.NET with little success

I started inherting DictionaryBase because of the Key retrieval ability, bu
my code bombed on the For Each line because I had Option Strict On and th
types didn't match. I now loop with an Object and then use CType, but that'
BS! >8-

Do I need to create my own Enumerator and overload Current()

C'mon Generics!!

Thanks in advance for help
Andrew J. Marshal
MCP (Visual Basic
Fairfax, V
 
N

Nick Hall

The DictionaryBase enumerator returns DictionaryEntry objects rather than
the objects you store in the collection. This allows you to have a
reference both to the object and its associated with key. So, to use
For-Each with a DictionaryBase-derived class you need to do something
similar to the following: -

For each de as DictionryEntry in MyCollection
Dim item as MyClass = DirectCast(de.Current, MyClass)

'....Use as necessary
Next de

Nick Hall
 
A

Andrew J. Marshall

Howler,

Thanks!

I actually started with CollectionBase, but I needed access to items by a
key so I switched to DictionaryBase. Any thoughts?

Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

The Howler said:
Try this...

(It is a collection of ContractSale objects). It will support the For...Each that you require

Public Class ContractSales
Inherits CollectionBase

Public Function Add(ByVal strBranchNo As String, ByVal intTillNo As Integer, _
ByVal strReceiptNo As String, ByVal
strCompositeReceiptNo As String, _
 
Joined
May 24, 2012
Messages
1
Reaction score
0
I've come up with a really simple solution to this:

Public Partial Class WebForm16
Inherits System.Web.UI.Page
'This shows how to use the ClassCollection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myCCol As New ClassCollection(Of Car)
Dim myCar As Car
myCar =
New Car
myCar.Name =
"Focus"
myCCol.Add(myCar, myCar.Name)
myCar =
New Car
myCar.Name =
"Cortina"
myCCol.Add(myCar, myCar.Name)
myCar =
New Car
myCar.Name =
"Astra"
myCCol.Add(myCar, myCar.Name)
myCar =
New Car
myCar.Name =
"Golf"
myCCol.Add(myCar, myCar.Name)
MsgBox(myCCol(1).Name)
MsgBox(myCCol(
"Astra").Name)
For Each myCar In myCCol
MsgBox(myCar.Name)
Next
End Sub
End
Class
'This is the definition of your class
Public Class Car
Public Name As String
End
Class
'This is a general class which you can put in a library for use whenever you want to create a ClassCollection
Public Class ClassCollection(Of T)
Inherits List(Of T)
Private KeyIndexCollection As New Collection
Overloads Sub Add(ByVal Obj As T, ByVal Key As String)
Me.Add(Obj)
KeyIndexCollection.Add(
Me.Count - 1, Key)
End Sub
Default Public Overloads ReadOnly Property Item(ByVal Key As String) As T
Get
Return Me(KeyIndexCollection(Key))
End Get
End Property
End
Class

 

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