Collection too slow

moongodess

Active member
Joined
Dec 23, 2004
Messages
40
I have two classes:

Code:
Friend Class clsItem
   Public Sub New(ByVal objId As Object, Optional ByVal objNr As Object = Nothing, _
      Optional ByVal objCode As Object = Nothing, Optional ByVal objDesc As Object = Nothing)
      Try
         Id = objId
         Nr = objNr
         Code = objCode
         Desc = objDesc
      Catch ex As Exception
         InfoError(ex)
      End Try
   End Sub

   Private mobjId As Object
   Public Property Id() As Object
      Get
         Return mobjId
      End Get
      Set(ByVal Value As Object)
         mobjId = Value
      End Set
   End Property

   Private mobjNr As Object
   Public Property Nr() As Object
      Get
         Return mobjNr
      End Get
      Set(ByVal Value As Object)
         mobjNr = Value
      End Set
   End Property

   Private mobjCode As Object
   Public Property Code() As Object
      Get
         Return mobjCode
      End Get
      Set(ByVal Value As Object)
         mobjCode = Value
      End Set
   End Property

   Private mobjDesc As Object
   Public Property Desc() As Object
      Get
         Return mobjDesc
      End Get
      Set(ByVal Value As Object)
         mobjDesc = Value
      End Set
   End Property
End Class

Code:
Friend Class clsItemCol
   Inherits CollectionBase

   Public ReadOnly Property Item(ByVal index As Integer) As clsItem
      Get
         Return CType(MyBase.List(index), clsItem)
      End Get
   End Property

   Public Sub Add(ByVal item As clsItem)
      MyBase.List.Add(item)
   End Sub

   Public Sub Remove(ByVal item As clsItem)
      MyBase.List.Remove(item)
   End Sub
End Class

This is supposed to be used as collections, to fill some combos with specific values.

Recently I verified that filling the collection is taking too long, and I dont know why. Im doing something like:

Code:
Dim colData As clsItemCol
colData = New clsItemCol

colData.Add(New clsItem(1, 3, "Item3", "Description3")
(...)

Does anyone have an idea, or some other way of doing this??
Thankx
 
How long is
exactly?

One thing you might want to do to speed things up is look at using a HashTable rather than a collection.
Also is there a reason why you have declared just about every single data type as object rather than the actual data type of the values you want to store?
 
Sometimes it is taking more than 2 seconds to fill a collection with 10 items, for example.

I declared everything as object because this is a generic colecction wich ill use many times, to different variable types. Do U think this can be a cause?

How do I use an HashTable? Im new at Vb.Net and I never heard about it :rolleyes:
 
If your goal is to fill a ComboBox, I would generally use an ArrayList to hold my collection of custom objects.

Still, 2 seconds is far too long. Is this confirmed with some kind of timing code? Also make sure you use Release Mode as your guide, Debug mode may take extra long sometimes. Also it would be nice if even your custom object could make some kind of assumption like an item has only an ID and Text property with maybe an optional "object" property to store more data.

Ive filled combos with literally thousands of custom classes (I wouldnt do it again - thousands is too many) and it always took less than 2 seconds. My class was a custom object with an Integer ID and a string Text property. I generally loop through a DataSet and create objects on the fly, filling an ArrayList that will get bound.

-ner
 
Back
Top