Solved

Passing ArrayList To A Class In Foreach Loop

Posted on 2006-11-02
6
328 Views
Last Modified: 2010-04-16
I am attempting to pass an ArrayList to a class, inside of a foreach loop. Each ArrayList is unique going in, however, after the loop, all ArrayLists have the same values.

I am doing:

foreach(myclass tempClass in myClassArrayList)
{
     ArrayList bla = tempClass.GetArrayList();
     //play around with bla, get it how it should be for this instance
     //at this point bla is unique
     tempClass.SetArrayList(bla);
}
//after this foreach, all myclass's in myClassArrayList have the same .GetArrayList() values, yet maintain different hash codes.



myclass.SetArrayList(ArrayList incoming)
{
      this.myClassesArrayList = new ArrayList(incoming);
}
0
Comment
Question by:GENTP
  • 3
  • 3
6 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 17859047
Can you post myclass.GetArrayList, and is myClassArrayList just an ArrayList?
0
 
LVL 5

Author Comment

by:GENTP
ID: 17859095
myClassArrayList is just an ArrayList.

myClass.GetArrayList is simply:

public ArrayList GetArrayList()
{
      return myClassArrayList;
}
0
 
LVL 21

Expert Comment

by:mastoo
ID: 17859348
A couple minor points.  Your GetArrayList is accessing a different member variable than the SetArrayList and the SetArrayList isn't declared as shown (i.e., myclass.SetArrayList)?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Author Comment

by:GENTP
ID: 17859419
It may just be my pseudo code...myclass looks like this.

public class myclass
{
       private ArrayList myClassesArrayList;

       public void SetArrayList(ArrayList incoming)
       {
             this.myClassesArrayList = new ArrayList(incoming);
       }
       pulbic ArrayList GetArrayList()
       {
             return myClassesArrayList;
       }
}

Calling code looks like this:

foreach(myclass tempClass in myClassArrayList)
{
     ArrayList bla = tempClass.GetArrayList();
     //play around with bla, get it how it should be for this instance
     //at this point bla is unique
     tempClass.SetArrayList(bla);
}
//after this foreach, all myclass's in myClassArrayList have the same .GetArrayList() values, yet maintain different hash codes.
0
 
LVL 21

Accepted Solution

by:
mastoo earned 500 total points
ID: 17860156
I copy/pasted your samples, added a static variable to keep track of the instances of myclass, added outputs after the loop to show what the arraylist holds, and got this output which shows them to be distinct.  Maybe run this sample and tell me if I'm misunderstanding the problem.

-- run this somewhere
      ArrayList myClassArrayList = new ArrayList();
      myClassArrayList.Add( new myclass() );
      myClassArrayList.Add( new myclass() );
      foreach(myclass tempClass in myClassArrayList)
      {
        ArrayList bla = tempClass.GetArrayList();
        //play around with bla, get it how it should be for this instance
        //at this point bla is unique
        tempClass.SetArrayList(bla);
      }
      System.Diagnostics.Debug.WriteLine( "Instance 0 says: " + myClassArrayList[0].ToString() );
      System.Diagnostics.Debug.WriteLine( "Instance 1 says: " + myClassArrayList[1].ToString() );
---
  public class myclass
  {
    private ArrayList myClassesArrayList;
    static int iCounter = 0;

    public myclass()
    {
      myClassesArrayList = new ArrayList();
      myClassesArrayList.Add( iCounter );
      myClassesArrayList.Add( iCounter + 100 );
      iCounter++;
    }

    public void SetArrayList(ArrayList incoming)
    {
      this.myClassesArrayList = new ArrayList(incoming);
    }

    public ArrayList GetArrayList()
    {
      return myClassesArrayList;
    }

    public override string ToString()
    {
      return "I am instance " + ((int)myClassesArrayList[0]).ToString();
    }

  }
0
 
LVL 5

Author Comment

by:GENTP
ID: 17860514
You are correct, that does work.

I was looking at the outcome and thinking that in my code every instance of myclass inside of myClassArrayList was pointing to the same myclass. What's actually happening, is every myclass is pointing to the same object (in this example it is an int, in my real code it's a custom object). Thus, when we simplify it and use ints, the above example works.

Thanks for the help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now