Collection Item(index as string)


Bill Ray

I have the following function in my WidgetCollection class.
What is a more efficient way of doing this?
I tried the commented code below, but I couldn't get it to work.

Public Overloads ReadOnly Property Item(ByVal index As String) As
Dim CurrentSetting, ReturnBO As Widget

For Each CurrentSetting In Me.InnerList
If CurrentSetting.DisplayID = index Then
ReturnBO = CurrentSetting
End If

Return ReturnBO
End Get
End Property

I know this is a dumb question, but does all this go in the class,
collection class or factory?
'Private Shared mProperties As
'Protected Friend Sub New(ByVal creator As BizObjectFactory)
' MyBase.New(creator)
' mProperties = System.ComponentModel.TypeDescriptor.GetProperties(GetType(Widget))
'End Sub
'Default Public Overloads Property Item(ByVal name As String) As
' Get
' Return mProperties(name).GetValue(Me)
' End Get
' Set(ByVal value As Widget)
' mProperties(name).SetValue(Me, value)
' End Set
'End Property

Stephen Muecke

If you are wanting to store items in a collection using a key (ie the
DisplayID property of the Widget), use a collection which derives from
This is an very fast way of retrieving items with a unique key

Public Class WidgetCollection
Inherits DictionaryBase

Default Public ReadOnly Property Item(ByVal ID As String) As Widget
'Gets the widget with the specified ID
Return DirectCast(MyBase.InnerHashtable(DisplayID), Widget)
End Get
End Property

Public Sub Add(ByVal widget As Widget)
'Add the widget to the collection using its ID as the key
'Use Dictionary methods to raise events
MyBase.Dictionary.Add(widget.DisplayID, organisation)
Catch ex As ArgumentException
'The widget already exists so replace it
MyBase.Dictionary.Item(widget.DisplayID) = widget
End Try
End Sub

Note: The following code might have been better (avoids having to continue
the loop once the item has been found)
Dim eWidget as Widget
For each eWidget in MyBase.InnerList
If eWidget.DisplayID = index
Return eWidget
End If


Bill Ray

We have many applications which inherit from our CoreCollectionClass
which inherits from CollectionBase. So changing to inherit from
dictionary class sounds risky.

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