Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 25th, 2004, 08:04 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old May 25th, 2004, 08:11 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
Reply With Quote
  #3 (permalink)  
Old May 25th, 2004, 10:10 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #4 (permalink)  
Old May 25th, 2004, 12:56 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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.
Reply With Quote
  #5 (permalink)  
Old May 25th, 2004, 02:05 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:26 PM.


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