Wrox Programmer Forums
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 25th, 2004, 08:04 AM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default ArrayList

hello
i have two classes

class1
{
 private ArrayList arr;

   public class1()
   {
    this.arr = new ArrayList();
    arr.add("bye");
    arr.add(.....);
    . . . .
    }

   void func1()
  {
   new class2(this.arr);
   }

   public ArrayList arr1
   {
   set{this.arr=value}
   get{return this.arr}
   }
}


class2
{
  class1 c1;
  private ArrayList arrCopy;

  public class2(ArrayList arr)
 {
  this.arrCopy = arr;
 }
........
void func()
{
c1.arr1.insert(0 , "hello");
}

my question is: suppose i change element 0 from "bye" to "hello" in class2 , why arrCopy is change element 0 too????
how can i avoid it??
thank you very much
koby

 
Old May 25th, 2004, 08:11 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

An ArrayList is an object. This line:

   this.arrCopy = arr;

does not create a copy of the array. It is assigning the object reference of "arr" to "this.arrCopy" so, as you have found, changes to one affect the other.

What you might be looking for is the Clone() method. This creates a shallow copy of the arraylist to another arraylist.


Peter
-------------------------
Work smarter, not harder
 
Old May 25th, 2004, 10:10 AM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi
i made a clone and its still the same problem
koby

 
Old May 25th, 2004, 12:56 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Hmmm.. Well that's no good then is it.

I looked in the docs and I didn't see any other method to copy an array. You might need to just copy the array manually element by element. Just keep in mind that any elements of the arraylist that are objects will still be the same instance.
 
Old May 25th, 2004, 02:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Fortunately, you can let the Framework do the copying for you. One overloaded constructor for an ArrayList accepts another ICollection, and all elements it contains are *copied* to the new ArrayList. So, instead of assigning one list to the other like this:
Code:
this.arrCopy = arr;
simply create a new ArrayList and pass in the old version:
Code:
this.arrCopy = new ArrayList(arr);
The following code is a working example that shows you what I mean:
Code:
// Create two List Instances
ArrayList List1 = new ArrayList();
ArrayList List2 = new ArrayList();

// Declare 3rd list for Constructor demo
ArrayList List3;

// Add items to List1
List1.Add("Hi");
List1.Add("Bye");

// Assign List1 to List2 
List2 = List1;

// Change second item in second list
// This should also affect List1
List2[1] = "New Bye";

// Both Message boxes should return New Bye
MessageBox.Show(String.Format("List1[1] is now {0}", List1[1]));
MessageBox.Show(String.Format("List2[1] is now {0}", List2[1]));

// Now use the overloaded contructor to
// *copy* the elements in List1 to List3
List3 = new ArrayList(List1);

// Change second item in third list. Was New Bye
// Is now Old Bye. Will *not* affect List1
List3[1] = "Old Bye";

// Now the MessageBox for List1 will return New Bye (the old value)
// while the listbox for List3 will return Old Bye (the new value)
MessageBox.Show(String.Format("List1[1] is now {0}", List1[1]));
MessageBox.Show(String.Format("List3[1] is now {0}", List3[1]));
Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList Mogg-Way C# 2005 5 February 12th, 2008 04:05 PM
ArrayList sort collie C# 1 August 28th, 2007 08:08 AM
ArrayList with UserControl rhd110 General .NET 2 August 12th, 2007 12:29 PM
ArrayList Problem erictamlam C# 2005 3 May 12th, 2007 10:04 AM
Help with an arraylist crazy-nun General .NET 4 July 14th, 2005 03:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.