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
|