Home All Groups Group Topic Archive Search About
Author
14 Apr 2005 12:32 PM
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?



--
--Eric Cathell, MCSA

Author
14 Apr 2005 12:48 PM
JohnFol
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









  "ECathell" <ecathell@nospam.mountaire.com> wrote in message news:O7TPK4OQFHA.1472@TK2MSFTNGP10.phx.gbl...
  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?



  --
  --Eric Cathell, MCSA
Author
14 Apr 2005 1:05 PM
Chris Dunaway
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.
Author
14 Apr 2005 2:03 PM
ECathell
yep got codesmith, I am still working through figuring it out..Thanks for
the tip...


--
--Eric Cathell, MCSA
Show quoteHide quote
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:1113483914.326447.189280@o13g2000cwo.googlegroups.com...
> 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.
>