views:

52

answers:

3

I am using the following code in C#. I am adding the values into the arraylist by using index. Now I want to read the values from the arraylist by using the index only. In the following example I am simply reading all the values from the arrylist but I want to read the values from the arrylist based on index( for e.g Customer_Details[i]) for each element at index i.

 public struct Cust_Info
        {
            public String Client_Key;
            public String Registration_Key;
            public int Standard;

            public Cust_Info(String C_Key, String Reg_Key, int Std)
            {
                Client_Key = C_Key;
                Registration_Key = Reg_Key;
                Standard = Std;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ArrayList Customer_Details = new ArrayList();
            for (int i = 0; i < 1; i++)
            {
                Customer_Details.Insert(i, new Cust_Info("A", "B", 1));
            }

            //for (int i = 0; i < 1; i++)
            //{
                Customer_Details.Insert(1, new Cust_Info("C", "D", 2));
                for (int i = 0; i < 1; i++)
                {
                    ArrayList obj=new ArrayList();
                    //((ArrayListOFStructures.Form1.Cust_Info)((new System.Collections.ArrayList.ArrayListDebugView(Customer_Details)).Items[0])).Client_Key
                    //obj = (ArrayList)Customer_Details[i];
                    foreach (Cust_Info temp in Customer_Details)
                    {
                        //comboBox1.Items.Add(Customer_Details[0].ToString());
                        comboBox1.Items.Add(temp.Client_Key);
                        comboBox1.Items.Add(temp.Registration_Key);
                        comboBox1.Items.Add(temp.Standard);
                    }
                }
        }

In the above code i want to make the use the structure only. How can I read the values from the arrylist based on index. Can you please provide me any code or link through which I can resolve the above issue ?

+1  A: 

I'm confused; you can get an item out of an ArrayList by index simply by:

Cust_Info cust = (CustInfo)theList[index];

However, ArrayList is pretty rare in anything >= .NET 2.0, a List<Cust_Info> would make this much easier. Also, Cust_Info looks to me very much like it should be a class (it is very rare to write a struct in .NET, and usually to denote "values" - a customer isn't a "value"). And public fields are also very much discouraged.

Note that currently you are (because it is a struct) actually copying the Cust_Info whenever you fetch it from (or place it in) the list; that isn't necessarily what you intend...

Marc Gravell
A: 

You can try something like

ArrayList arr = new ArrayList();

for (int iIndex = 0; iIndex < arr.Count; iIndex++)
{
    object o = arr[iIndex];
}

But I would rather go with

List Class and List.Count Property

astander
A: 
for(int i=0; i<Customer_Details.Count/*or.Length*/; i++)
Customer_Details[i] = something;
Ahmad Farid