Searching an ArrayList

IrishChieftain picture IrishChieftain · Jun 20, 2011 · Viewed 23.4k times · Source

I'm working on some legacy code, so cannot use Generic List here. I have an ArrayList being returned from a data layer method. Each item in the last consists of an ID and a Description field. I want to loop through the ArrayList and search for a match on the Description string - any ideas?

Format

ID    DESCRIPTION
1     SomeValue

I know I can do this:

bool found = false; 
if (arr.IndexOf("SomeValue") >= 0) 
{
    found = true;    
}

But is there a way to do a string compare for a particular Description value?

UPDATE

Amended version of Seattle Badger's answer:

for (int i = 0; i < arr.Count; i++)
{
    if (arr[i].ToString() == "SomeValue")
    {
        // Do something
        break;
    }
}

Answer

agent-j picture agent-j · Jun 20, 2011
bool found = false;
foreach (Item item in arr)
{
   if ("Some Description".Equals (item.Description, StringComparison.OrdinalIgnoreCase))
   {
      found = true;
      break;
   }
}