0

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 ?

4
  • 1
    ArrayList should not be used. Use List<T> instead. Commented Oct 13, 2010 at 12:43
  • 1
    What is the point of for (int i = 0; i < 1; i++)? Commented Oct 13, 2010 at 12:43
  • The code is very confusing. It is not clear what you are trying to achieve or what, exactly, the problem is. You create an array-list, populate it, then insert an additinoal record. Then, in a loop of one iteration you create a new, unused array-list, then add several items to a combo for each Cust_Info structure in your arraylist. What is the result you desire? Commented Oct 13, 2010 at 12:45
  • Vommit.. Have you jumped into Visual C# or C# for dummies. I remember trying to teach new devs c#, but they want to get winforms up and running via those books or VTC in a few hours, disasters* like the above were all too common - and honestly that's what it is (Im telling you truthfully - downvote if you will - its an f'ing disaster). I dont think correcting the code will do much good, you need to withdraw and realize you wont be delivering business value to your employee or your own project until you've got the basics down. Start with Jesse Liberty book... Commented Oct 13, 2010 at 16:35

3 Answers 3

3

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...

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.