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

B

Bradley

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
 
R

Rick Lones

Bradley said:
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-
 
G

Guest

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
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top