PC Review


Reply
Thread Tools Rate Thread

Custom collection: how to find item in collection using custom indexer

 
 
panik
Guest
Posts: n/a
 
      21st Aug 2003
Hi,

I have a custom collection that implements CollectionBase. The collection is
called Sensors and contains a list of Sensor objects.

There is the usual index using an integer (Sensors[int index]).

Is there a way of using a custom indexer (for example Sensors[string name])
that does *not* need to loop through each item in the collection and compare
them?

I put this code in the get{} of my custom indexer, trying to find the item
in the collection with the specified name:

for(int i=0 ; i<this.List.Count ; i++)
{
if(((Sensor)this.List[i]).Name==name)
{
return (Sensor)this.List[i];
}
else
{
return null;
}
}

First, I'm worried about the consecutive conversion in the if() statement.
Second, my get statement throws an error when compiling saying 'not all code
paths return a value'.

Is the consecutive conversion overkill? Is there another way of dealing with
this custom indexer(GetValue,...)?
How can I force the get statement to return a value?

thank you for the help,
vincent


 
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
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
<?>Indexer Rises from the Disabled or "Night of the Locking Indexer"<?> citizenkahn Windows XP Help 0 26th Jun 2006 04:47 AM
Pleas help - collection property indexer worries... almurph@altavista.com Microsoft C# .NET 1 30th Jun 2005 05:00 AM
Extending a Collection indexer Matt Microsoft C# .NET 4 19th Nov 2003 08:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:27 PM.