Hi,

I'm having problem when trying to read from ArrayList.Here is my code

ArrayList arrList;

public ARMakbuz( ArrayList Alist)
 {
             arrList =new ArrayList( Alist);
 }

private void ARMakbuz_ReportStart(object sender, EventArgs e)
        {
            foreach (ArrayList item in arrList) //Here I get exception
            {
                Fields["KOD"].Value = item[0];
                Fields["DESC"].Value = item[7];
                Fields["QTY"].Value = Convert.ToInt32(item[1]);
                Fields["PRICE"].Value = Convert.ToDouble(item[3]);



            }

I get " System.String[] type object could not be assigned to System.Collection.ArrayList" exception. I appreciate any help.

Thanks,
snky

foreach(ArrayList item in arrList)

The above line suggests that arrList stores more ArrayList objects. However, your exception indicates that the contents of arrList are actually string arrays. Your code could be the following:

foreach(string[] item in arrList)

Which would only cause an exception if there was something in the arrList that was not a string array. Another more explicit version that would function in the event of objects that are not string arrays is the following:

foreach (object obj in arrList)
{
    if (obj is string[])
    {
        string[] item = (string[])obj;
        // do more work
    }
}

Like previously said it depends what is in your arrayList. if it is multiple arrays in your list then you would want to do

ArrayList ary = new ArrayList();

            foreach (Array item in ary)
            {
                  //do something here
            }

In short, you want to do foreach WHATEVER instance you have in SOMETHING

foreach(string[] item in arrList) line solves my problem. I had previously used it "string" w/o wbrackets which I had exception.

Thank you for your help
snky

Actually SharpJohnnyG, apegram showed the better way to do it.
The items stored in an arrayList are stored as objects as the arraylsit is not type specific. As such, your code would throw an exception if one of the items in the arraylist was not an array.
By iterating through them as objects (the way apegrams last code block does) then checking the type, you ensure that you handle the item correctly.

Generally, if you are coding for .net2.0 or later it is recommended that you use the generic collections rather than the old non-generic ones like the ArrayList.

The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible instead of classes such as ArrayList in the System.Collections namespace.

foreach(string[] item in arrList) line solves my problem. I had previously used it "string" w/o wbrackets which I had exception.

Thank you for your help
snky

Glad you were able to fix your problem. Be aware though, that this will only work as long as all the items in the arraylsit are string arrays. The arraylsit will allow you to store an item of a different type without warning and will throw an exception when your foreach loop tries to cast it to a string array.

I appreciate your concern and I agree with the merits of using object based arraylist iteration.

Thanks again
snky

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.