Custom collections

  • Thread starter Thread starter ECathell
  • Start date Start date
E

ECathell

Collections are really kicking me hard.

I have a class Box. It has 3 properties(Boxtype,BoxTare,BoxDescription)

I also want to make a BoxCollection object. I have tried using collectionbase and arraylist, but I can't figure out how to "make it work" the way I think it should.


Public Class BoxCollection
Inherits ArrayList

Public Sub New()

End Sub

Private mBoxNames As ArrayList
Public ReadOnly Property BoxNames() As ArrayList
Get
Return mBoxNames
End Get
End Property
Public Function getBoxNames(ByVal location As String) As BoxCollection
Dim db As dal.Database = Nothing
db = dal.DatabaseFactory.CreateDatabase(location)

Dim reader As IDataReader
Dim commandtext As String = "Select Boxtype from boxes"
reader = db.ExecuteReader(CommandType.Text, commandtext)
While reader.Read
mBoxNames.Add(reader(0))

End While


End Function



This is one of my attempts. I feel like I have failed miserably.Can anyone point me to good tutorials on collections and building them? Or should I take another avenue since its attached to a database?
 
If you already have the class called Box, to make a collection of Box's you need this sort of setup

Friend Class Boxes



Inherits System.Collections.CollectionBase

Public Sub Add(ByVal oBox As Box)

list.Add(oBox)

End Sub

Public Sub Remove(ByVal index As Integer)

If index > Count - 1 Or index < 0 Then

'Not found so do nothing

Else

list.RemoveAt(index)

End If

End Sub

Default Public ReadOnly Property Item(ByVal index As Integer) As Box

Get

Return CType(List.Item(index), Box)

End Get

End Property

Public Sub Dispose()



list.Clear()

End Sub

End Class




In the code that makes use of the Boxes, you do this sort of thing.

Dim myBoxes as Boxes
myBoxes.Add (myBox)


etc.

Hope this helps









Collections are really kicking me hard.

I have a class Box. It has 3 properties(Boxtype,BoxTare,BoxDescription)

I also want to make a BoxCollection object. I have tried using collectionbase and arraylist, but I can't figure out how to "make it work" the way I think it should.


Public Class BoxCollection
Inherits ArrayList

Public Sub New()

End Sub

Private mBoxNames As ArrayList
Public ReadOnly Property BoxNames() As ArrayList
Get
Return mBoxNames
End Get
End Property
Public Function getBoxNames(ByVal location As String) As BoxCollection
Dim db As dal.Database = Nothing
db = dal.DatabaseFactory.CreateDatabase(location)

Dim reader As IDataReader
Dim commandtext As String = "Select Boxtype from boxes"
reader = db.ExecuteReader(CommandType.Text, commandtext)
While reader.Read
mBoxNames.Add(reader(0))

End While


End Function



This is one of my attempts. I feel like I have failed miserably.Can anyone point me to good tutorials on collections and building them? Or should I take another avenue since its attached to a database?
 
One suggestion is to use CodeSmith which is a code generator. It comes
with templates to create different type of code.

There are several templates that create collection classes. You just
specify the type of object to make the collection from and it generates
a complete class. Best of all, its free.
 

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

Back
Top