Solved

Passing ArrayList To A Class In Foreach Loop

Posted on 2006-11-02
6
344 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

627 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