4

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;
    }
}
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91

5 Answers5

3
bool found = false;
foreach (Item item in arr)
{
   if ("Some Description".Equals (item.Description, StringComparison.OrdinalIgnoreCase))
   {
      found = true;
      break;
   }
}
agent-j
  • 27,335
  • 5
  • 52
  • 79
  • For some reason, not getting intellisense for the Item property of the ArrayList. Am using the System.Collections directive - this is a library class... – IrishChieftain Jun 22 '11 at 15:01
  • What kind of object is in your array? My code above assumes the type is `Item`, but you can substitute the class name for `Item`. – agent-j Jun 22 '11 at 15:04
  • It's an ArrayList being returned from a SPROC. Each object consists of an ID and Description field.. maybe author should have used a DataSet? Either way I'm stuck with the ArrayList. Tried substituting Object but getting "object does not contain a definition for Description" error message... – IrishChieftain Jun 22 '11 at 15:12
  • 1
    Try the example @Bala R gave. If you really want it to be early bound(verifiably correct at compile-time), use `foreach(object item in arr) Console.WriteLine (item.GetType().FullName + " " + (item.GetType().IsPublic ? "public" : "private"));`. If the type is public, use the full name in the foreach statement from my post. – agent-j Jun 22 '11 at 15:22
1

I may be missing something in your question, as this seems pretty straight forward to me. But then I'm pretty old-school....

Does this help?

protected void Page_Load(object sender, EventArgs e)
{
    ArrayList arrSample = new ArrayList();

    // populate ArrayList
    arrSample.Items.Add(0, "a");
    arrSample.Items.Add(1, "b");
    arrSample.Items.Add(2, "c");

    // walk through the length of the ArrayList
    for (int i = 0; i < arrSample.Items.Count; i++)
    {
        // you could, of course, use any string variable to search for.
        if (arrSample.Items[i] == "a")
            lbl.Text = arrSample.Items[i].ToString();
    }
}

As I say, not sure if I'm missing something in your question. badger

Farhan
  • 2,535
  • 4
  • 32
  • 54
Seattle Badger
  • 308
  • 1
  • 2
  • 6
  • Marked as answer.. I tweaked code a little because ArrayList does not have an Items property... see update in original post. Thanks! :-) – IrishChieftain Jun 22 '11 at 15:56
0

If you have an ArrayList try the "Contains" or "BinarySearch" built-in functions of ArrayLists.

protected void Page_Load(object sender, System.EventArgs e)
    {
    ArrayList alArrayList = new ArrayList();
    alArrayList.Insert(0, "a");
    alArrayList.Insert(1, "b");
    alArrayList.Insert(2, "c");
    alArrayList.Insert(3, "d");
    alArrayList.Insert(4, "e");

    //Use Binary Search to find the index within the array
    if (alArrayList.BinarySearch("b") > -1) {
            txtTemp.Text += "Binary Search Array Index: " + alArrayList.BinarySearch("b").ToString;
    }

    //Alternatively if index not needed use Contains function
    if (alArrayList.Contains("b")) {
            txtTemp.Text += "Contains Output: " + alArrayList.Contains("b").ToString;
    }
}
MAlvarez
  • 86
  • 5
0
foreach(object o in arrayList)
{
    var description = o.GetType().GetProperty("Description").GetValue(o, null);
    if("some description".Equals(description) )
    {
       //do something
    }

}
Bala R
  • 107,317
  • 23
  • 199
  • 210
0

Are you sure you can't use LINQ? What version of the Framework are you running?

Just because this is not a Generic type doesn't mean you can't make it such. Consider arr.Cast(of YourType).Where(...).

mtazva
  • 1,005
  • 9
  • 13