Results 1 to 10 of 10

Thread: [1.0/1.1] Copying an ArrayList to another

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    [1.0/1.1] Copying an ArrayList to another

    Hi. I'm passing an arraylist to a method in which I want to copy it. I did it as:

    ArrayList al....

    add values to al....

    call method: method(al)

    ...


    another class:
    ArrayList te;

    .....

    public void method(ArrayList al)
    { te = al;
    }


    Now it is assigning, but if I'm back in the original class and I change ArrayList al, the value of te is also changing. Does anyone knows how to fix this problem? I just want to copy the value of al to te in the method, not the actual pointer to the ArrayList.

  2. #2

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [1.0/1.1] Copying an ArrayList to another

    This is working:

    for(int x=0 ; x<al.Count ; x++)
    {
    this.te.Add(al[x].ToString());

    }


    But is there a shorter way of doing this?

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [1.0/1.1] Copying an ArrayList to another

    use:
    Code:
    mySourceArray.CopyTo(myTargetArray, Index);
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [1.0/1.1] Copying an ArrayList to another

    Quote Originally Posted by ComputerJy
    use:
    Code:
    mySourceArray.CopyTo(myTargetArray, Index);

    Its:

    Array.Copy(source, 0, Dest, 0, source.count);


    But you have to convert the arrayLists as Arrays. I don't want to convert the ArrayLists to arrays, I want when it is finished copying, the dest and source are still both arraylists.

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [1.0/1.1] Copying an ArrayList to another

    No, I'm talking about srcArray.CopyTo()
    it's not a static method, so you can call it from an instance of the ArrayList
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [1.0/1.1] Copying an ArrayList to another

    I'm getting this error:

    Argument '2': cannot convert from 'System.Collections.ArrayList' to 'System.Array'


    from this code:

    Main.CopyTo(0, CopyValue, 0, Main.Count);


    where main is the source arraylist and CopyValue is the destination arraylist.
    I also tried

    Main.CopyTo(CopyValue, 0);

    But I was getting the following error

    The best overloaded method match for 'System.Collections.ArrayList.CopyTo(System.Array, int)' has some invalid arguments

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [1.0/1.1] Copying an ArrayList to another

    Sorry, it seems that CopyTo only copies into an array.
    use:
    Code:
    ArrayList b=arrayListA.Clone();
    This is supposed to work (hopefully)
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [1.0/1.1] Copying an ArrayList to another

    or you can do this:
    Code:
    arrayListB = arrayListA.GetRange(0, arrayListA.Count);
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [1.0/1.1] Copying an ArrayList to another

    I got figured it out:

    ArrayList al....

    add values to al....

    call method: method(al)

    ...


    another class:
    ArrayList te;

    .....

    public void method(ArrayList al)
    { // te = al; -> this is wrong since it would copy the actual pointer to al, so
    // if I change te, al will also be changed.

    te = new ArrayList(al); // This is copy by value only not pointers!
    }


    Thanks computer jr

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/1.1] Copying an ArrayList to another

    You should use the Clone method as cj suggested in post #7. Clone is the standard way to create a shallow copy of an object, assuming that the type implements IClonable, which Clone is a member of.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width