473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
Nov 20 '05 #1
3 2867
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

Nov 20 '05 #2
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
"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> wrote
in message news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
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, _ 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 ContractSale
Dim objNew As New ContractSale

With objNew
.BranchNo = strBranchNo
.TillNo = intTillNo
.ReceiptNo = strReceiptNo
.CompositeReceiptNo = strCompositeReceiptNo
.ContractID = intContractID
.Barcode = strBarcode
.SaleDate = dteSaleDate
.Surname = strSurname
.MobileNo = strMobileNo
.IMEI = strIMEI
.SIM = strSIM
.Network = strNetwork
.ImageFileName = strImageFileName
End With

list.Add(objNew)
Add = objNew

End Function
Public Sub Remove(ByVal intIndex As Integer)
list.RemoveAt(intIndex)
End Sub

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

End Class

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

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

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Haney | last post by:
Postgresql 7.4.3-1 under Cygwin. I created a table called ServerTypes: CREATE TABLE ServerTypes( ServerTypeID SERIAL UNIQUE NOT NULL, Type TEXT PRIMARY KEY); Works fine.
3
by: anon | last post by:
I have been used to using DAO in the past, and then converted to ADO. Now I am having to use VB.Net(2000) and ADO.NET and am experiencing difficulties with the creation and population of an mdb....
4
by: Alex Page | last post by:
This is probably really basic, but I can't seem to get it to work. I'm trying to create an enumerated type, using the following code: CREATE FUNCTION enum_gender_in (cstring) RETURNS enum_gender...
9
by: jab | last post by:
Je veux lier (join) une table qui se trouve dans une database avec une qui se trouve dans une autre database. Les 2 databases sont sur le même serveur en l'occurence DB2/NT 7.2.9. J'ai créé un...
1
by: Vinay Jain | last post by:
Hi I am newbie so this problem may be too simple to be asked.please help me if any new thing to be added in following: I want to use user defined data type in User_Type(n) way.... I created...
0
by: DotDidIt | last post by:
Hi Everybody! I developed a Web service with IBM RAD v 6.0.1. After creating the WSDL file i have tried to develop a .Net client. But by using wsdl.exe (1.1.4322) to create a .net c# proxy i...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
6
by: windandwaves | last post by:
Hi Folk Some of my clients asked me to create "fancy emails" for them (aka html formatted emails). I know how to make a nice html document, but I had trouble creating a simple way to provide...
2
markmcgookin
by: markmcgookin | last post by:
Hi Folks, Bear with me if this sounds stupid pls! I have an xsl doc that is translating one type of xml to another (still with me?) I was wondering, is it possible to do the same thing to...
1
by: ebusiness | last post by:
I am trying to create nicknames for tables in a remote database. The federated database and remote database are both DB2 UDB V8.2, both OS are AIX. Could anyone tell me how to create a nickname...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.