Solved

Passing ArrayList To A Class In Foreach Loop

Posted on 2006-11-02
6
330 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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