Bill,
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
DictionaryBase.
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
Get
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
Try
'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
Next
Stephen
"Bill Ray" <ri***@mail.com> wrote in message
news:63*************************@posting.google.co m...
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
Widget
Get
Dim CurrentSetting, ReturnBO As Widget
For Each CurrentSetting In Me.InnerList
If CurrentSetting.DisplayID = index Then
ReturnBO = CurrentSetting
End If
Next
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
System.ComponentModel.PropertyDescriptorCollection
'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
Widget
' Get
' Return mProperties(name).GetValue(Me)
' End Get
' Set(ByVal value As Widget)
' mProperties(name).SetValue(Me, value)
' End Set
'End Property