Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Passing ArrayList To A Class In Foreach Loop

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
GENTP
Asked:
GENTP
  • 3
  • 3
1 Solution
 
mastooCommented:
Can you post myclass.GetArrayList, and is myClassArrayList just an ArrayList?
0
 
GENTPAuthor Commented:
myClassArrayList is just an ArrayList.

myClass.GetArrayList is simply:

public ArrayList GetArrayList()
{
      return myClassArrayList;
}
0
 
mastooCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
GENTPAuthor Commented:
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
 
mastooCommented:
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
 
GENTPAuthor Commented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now