PC Review


Reply
Thread Tools Rate Thread

Collection Item(index as string)

 
 
Bill Ray
Guest
Posts: n/a
 
      17th Nov 2004
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
 
Reply With Quote
 
 
 
 
Stephen Muecke
Guest
Posts: n/a
 
      17th Nov 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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



 
Reply With Quote
 
Bill Ray
Guest
Posts: n/a
 
      17th Nov 2004
We have many applications which inherit from our CoreCollectionClass
which inherits from CollectionBase. So changing to inherit from
dictionary class sounds risky.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting up a shared database ("index specified does not reference an existing item in the collection" errors) Boby George Windows XP Embedded 0 11th Apr 2008 02:48 AM
Is there a way of getting the index of an item in a collection of ToolStripMenuItems? Academia Microsoft VB .NET 5 26th Feb 2008 04:49 PM
how can i get the index of a selected listview item and index a related arraylist item? forest demon Microsoft C# .NET 2 10th Nov 2006 05:45 AM
Why can I get the index of the item of the array when I use string*, but can not get the index of the array when I use any other type (such as Int32)? Allen Maki Microsoft VC .NET 1 20th Mar 2006 10:44 AM
key/value collection that allows key string to be updated and retains collection item entry order dx Microsoft Dot NET Framework 2 25th Sep 2004 05:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:50 PM.