Problems with ArrayLists

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi There,
I encountered Problems using ArrayList objects.
My question is:
How can I search members of objects (i.e. object.Member) in my ArrayList???
Purpose: I want to see if I have objects of a certain address in my list,
and if not,
i want to create some accordingly and append them to the list.

In my project I have:
----------------------------------------------------------------------
class CElement
{
public uint Address;
public string Data;
public string OtherData;
....
}

ArrayList arrayList = new ArrayList();
....
CElement obj = new CElement();
obj.Address=1234;
....
arrayList.Add(obj);
....
if ( arrayList.Contains(obj.Address) ) //this does not work
CElement match = (CElement) arrayList[arrayList.IndexOf(obj.Address)]
 
Hi Ginny, excuse me for being a bit chuckleheaded ;-) life can be so simple.
Of course this would be _the_ solution and absolutely do the job,
as we can see here: (Examine each relevant object member in an ArrayList and
give matches back or create some.)

regards Chris
 
Chris,

Yep, we all have those moments. ;-) Don't forget to exit your foreach loop
once you've found a match.
 
ok to be precise, it 'd be suitable to add a "return;" statement in the
branch where a match was found. ;-)
But as the list is only populated by different elements,
it would only be a matter of performance;

regards
Chris
 
Back
Top