473,467 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to access index of ArrayList based on stored object's property value

Hi

I have an ArrayList populated with some objects. Each object has some properties, and I'd like to find the ArrayList's index where the object's "SerialNumber" property is equal to a certain integer. This doesn't seem as easy as using a regular Array's IndexOf() method, since I need to access the SerialNumber property of the object stored in the ArrayList. It would be nice to just find the correct index of myArrayList without looping through the whole ArrayList and comparing each object's "SerialNumber" property to my integer with code.

Is this possible, and if so can anyone show me an example or a link to a good resource?

Thanks, Brad

Nov 17 '05 #1
3 1992
You could write a custom IComparer and use the BinarySearch method:

http://msdn.microsoft.com/library/de...archTopic3.asp

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Bradley" wrote:
Hi

I have an ArrayList populated with some objects. Each object has some properties, and I'd like to find the ArrayList's index where the object's "SerialNumber" property is equal to a certain integer. This doesn't seem as easy as using a regular Array's IndexOf() method, since I need to access the SerialNumber property of the object stored in the ArrayList. It would be nice to just find the correct index of myArrayList without looping through the whole ArrayList and comparing each object's "SerialNumber" property to my integer with code.

Is this possible, and if so can anyone show me an example or a link to a good resource?

Thanks, Brad

Nov 17 '05 #2
Bradley wrote:
Hi

I have an ArrayList populated with some objects. Each object has some
properties, and I'd like to find the ArrayList's index where the
object's "SerialNumber" property is equal to a certain integer. This
doesn't seem as easy as using a regular Array's IndexOf() method, since
I need to access the SerialNumber property of the object stored in the
ArrayList. It would be nice to just find the correct index of
myArrayList without looping through the whole ArrayList and comparing
each object's "SerialNumber" property to my integer with code.

Is this possible, and if so can anyone show me an example or a link to a
good resource?


It sounds like what you really want is a SortedList, which can be accessed
either by index as per a normal array or by fast (binary) key search. I would
recommend you take a look at this class unless you are stuck for some reason
with the ArrayList.

HTH,
-rick-
Nov 17 '05 #3
it might be easier to create a class which inherits from ArrayList (or
whatever) since only in this way the internal storage could be changed or
accessed.

public class MyList: ArrayList {

public override int IndexOf(object value) {
//return base[index];
//do your magic
}

}
Nov 17 '05 #4

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

Similar topics

10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.