CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2012
    Posts
    7

    How to copy a list to another?

    I have two same types of lists: list1 and list2. and the program has a structure as follows. each i loop the list1 size changes. I want to use list2 to keep data in list1 before calling func5() if condition1 is satisfied. how can I do this? Thanks

    The code structure:
    main()
    {
    List<class>List1 = new List<class> ();
    loadup List1;

    List<class>List2 = new List<class> ();
    for int i=1; i<=5;i++
    {
    ...

    if(!func(ref List1))
    {
    ...
    }

    ...
    }

    bool func(ref List<class>list1)
    {
    func1(ref list1);
    func2(ref list1);

    if (condition1 is true)
    {
    func3(ref list1);
    func4(ref list1);
    func5(ref list1);
    ...
    }
    return true;
    }

  2. #2
    Join Date
    Oct 2011
    Posts
    97

    Re: How to copy a list to another?

    Just copy every element from list1 to list2. There might be a better way, but this will definitely work.

    Code:
    list2.Clear();
    for(int i=0;i<list1.Count;++i)
    {
    	list2.Add(list1.Item(i));
    }

  3. #3
    Join Date
    Apr 2012
    Posts
    7

    Re: How to copy a list to another?

    Thanks for reply. I add one more function as:

    bool PulsesCopy(ref List<class1> list1,
    ref List<class1> list2)

    {
    list2.Clear();
    for (int i = 0; i < list1.Count; ++i)
    {
    list2.Add(list1[i]);
    }
    return true;
    }

    and change func (ref list1) to func(ref list1, ref list2)
    and in the body of func(ref list1, ref list2)
    before call func5(ref list1), the following code is added:

    PulsesCopy(ref List<class1> list1, ref List<class1> list2).

    so list2 should keep list1 data before calling func5(). since func5(ref list) updates list1, after calling func5(ref list1), list1 and list2 are different. However, I found list2 changed too after calling func5(ref list1). i.e., list2 = list1. what's wrong in my code?

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How to copy a list to another?

    It's because C# has value types (structs) and reference types (classes).
    Value types (like bool, float, double, int, long...) will always be passed around by value (a copy is made), while reference types are passed by reference (the variable refers to the original object).

    This means that what Access_Denied suggested would work with value types - the two arrays would contain two distinct sets of values. But with reference types, as in your case, that code results in a shallow copy (two array objects referencing the same set of elements). Note that when mutating the elements, both arrays are affected, but when replacing/removing an element in one array, then nothing changes in the other.

    So you need to modify your class to provide a method that enables it to copy itself, or provide a constructor that makes a deep copy of an existing object.

    Say you added a Copy() method. You would then create the second list like this:
    list2.Add(list1[i].Copy());
    Last edited by TheGreatCthulhu; April 24th, 2012 at 08:15 PM.

  5. #5
    Join Date
    Apr 2012
    Posts
    7

    Smile Re: How to copy a list to another?

    Thank you very much. I used Clone() and it works now.

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How to copy a list to another?

    Be careful with Clone(): always read the MSDN documentation for the specific class you're working with - clone can be implemented as shallow copy for some types (for example, calling Clone() on arrays types creates a shallow copy).

  7. #7
    Join Date
    Apr 2012
    Posts
    7

    Re: How to copy a list to another?

    Quote Originally Posted by TheGreatCthulhu View Post
    Be careful with Clone(): always read the MSDN documentation for the specific class you're working with - clone can be implemented as shallow copy for some types (for example, calling Clone() on arrays types creates a shallow copy).
    Thanks for the reminding. I will be careful using Clone(). the reason I use the Clone() is because there is no Copy() available for my case.

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

Featured