Solved

Passing ArrayList To A Class In Foreach Loop

Posted on 2006-11-02
6
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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