473,836 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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

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.Cou nt ; i++)
if(((Sensor)thi s.List[i]).Name==name)
return (Sensor)this.Li st[i];
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(GetValu e,...)?
How can I force the get statement to return a value?

thank you for the help,
Nov 15 '05 #1
0 4086

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: pantichd | last post by:
<I also posted this on microsoft.public.vsnet.debugging 'cuz I didn't know which was more appropriate or would get quickest response> Hello, Can someone tell me how I can view the contents of a collection when in debug mode? For example, I working with a dataset and when the app stops at a breakpoint
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx. The problem is the article is in VisualBasic. I already get the collection to be recognized as a Data Source by the IDE. It populated the DataGrid correctly from the fields on the items object of the collection, but I can't get the DataGrid to...
by: jamie_m_ | last post by:
I have a custom collection ... clFile that INHERITS from NameObjectCollectionBase the problem is, when I try to create an xmlserializer instance i get an error You must implement a default accessor on brWAP.clFile because it inherits from ICollectio I must be having a thick day because I dont have a clue what the error message means. (I have written a defaut property called ITEM in the clFile class 'Classes to abstract a FILE entity...
by: Gerald Lightsey | last post by:
I am doing some work that involves automating MS MapPoint 2002. The object model is documented in the context of Visual Basic 6.0. A typical example follows. 'Output first result of find search Set objFindResults = objApp.ActiveMap.FindAddressResults("One Microsoft Way", "Redmond", , "WA", , geoCountryUnitedStates) MsgBox "The first item in the find list is: " _ + objFindResults.Item(1).Name
by: Gary van der Merwe | last post by:
Hi I want to write my own collection object. I want it to have the following features. 1.. It must be strongly typed (to a Class that I have written). 2.. I should be able to add and remove items dynamically. 3.. It should have an indexer. This will have a string parameter. The
by: Tinus | last post by:
Hello all, I've create a custom control (UserControl) and have a custom Item Collection. The control is a custom calendar which is draw using the Graphics Rectangle etc. functions. It is drawn when the control is painted or resized. When the control is drawn it draws also the items found in the collection. So far so good.... I have 3 questions which I'm unable to find a solution
by: Don | last post by:
I've been trying to create a custom collection that can be bound to a combobox via the DataSource property, but I can't seem to get it to work. I've created a class that implements IList, and it seems to work okay on its own, but when I try to bind it to a combobox, the combobox doesn't show anything (and it starts having problems repainting itself). The Datasource appears to contain my custom collection object, but when I ask for its item...
by: Erick | last post by:
I've created a class called Procs and a collection class called Processes which uses a hastable object to store the Procs. Now i want to enumerate with the "For each" to extract all the Procs in my Processes class. As far as i can tell i need to implement an IEnuerator method to do this. But how ? 'Procs Class Public Class Procs Private _Id As Integer
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int, would return the value of obj because it returns the index of the item at position obj. Thoughts I had were to let the default method perform the default behavior and then create an overload as IndexOf(int index, ProductSearchCriteria criteria)...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.