CodeSmith Community
Your Code. Your Way. Faster!

ArrayList deep copy?

Latest post 11-22-2005 10:26 PM by anewyoung. 5 replies.
  • 02-25-2004 8:06 PM

    ArrayList deep copy?

    I'm looking at using the ArrayList template to generate custom entity collection classes, but am running into a snag - the lack of support for deep copies, which I need for creating original (backup) versions of any template containing one or more collections of other entities.
     
    What I had hoped for was that the Clone method would do a deep copy, or that a method named something like DeepCopy (or just Copy, since Clone does a shallow copy) would exist in the template.
     
    Chris, Eric? Any thoughts on this?  Am I barking up the wrong tree?
     
    Thanks,
     
    Oskar
    Thanks,
    • Post Points: 205
  • 02-25-2004 8:54 PM In reply to

    RE: ArrayList deep copy?

    Oskar,

    Look at the CSLA templates. Specifically look at the BusinessBase and BusinessCollectionBase classes. These contain a Clone method that does a deep copy.

    HTH!

    Slartibartfast
    fjord Engineer ;)
    So as a rule you can recognize genuinely smart people by their ability to say things like "I don't know," "Maybe you're right," and "I don't understand x well enough."
    • Post Points: 5
  • 02-26-2004 12:20 PM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: ArrayList deep copy?

    Oskar,

    The problem is that there's no standardized "deep copy" functionality in the .NET Framework. The standard collection classes don't provide deep copies for the same reason.

    Shallow copying is the only thing that's guaranteed to work because you just copy the element references. Anything else has to rely on the assumption that the specific type in question, and any other types it might reference, provides deep copies of itself via ICloneable.Clone (or in some other way).

    ICloneable.Clone on any collection template definitely will never do a deep copy, for how then would you create a shallow copy if you wanted one? Providing another method that attempts to call ICloneable.Clone on all the elements would be possible, though.

    Browsing the MSDN Library, I see that a couple of types provide a Copy method to make deep copies of themselves. I suppose that would be a possible enhancement for a future release.

    Cheers, Chris
    • Post Points: 5
  • 02-27-2004 11:36 AM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: ArrayList deep copy?

    Okay, I've thought about it and realized that I need a deep copy anyway, so I'll put it in the next release. The method is named Copy, and it invokes a customizable method on the ItemType elements. The templates detect when you try to deep-copy value types or strings, and do a shallow copy instead automatically.

    For dictionary collections, only the values are copied in this way; the keys are always just cloned with a shallow copy. That should work well enough for the majority of cases where the key is a value type or a string, and avoids the pitfall of trying to index a copied dictionary with a key from the original dictionary.
    • Post Points: 5
  • 02-27-2004 2:11 PM In reply to

    RE: ArrayList deep copy?

    Chris, thanks. Looking forward to the next release.

    Oskar
    Thanks,
    • Post Points: 5
  • 03-01-2004 7:44 AM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: ArrayList deep copy?

    Deep copies for all collection classes are in the new version 1.5.0. Let me know if there are any problems.

    Cheers, Chris
    • Post Points: 5
  • 01-10-2005 12:16 AM In reply to

    RE: ArrayList deep copy?

    A deep copy also seems to be performed in standard C# statements while performing a memberwise copy like so:
    (assuming that ArrayListToCopy contains the original data and u cast the type back to the original content of the array)

    ArrayList arrayToCopyTo = new ArrayList();
    for(int i = 0; i < ArrayListToCopy.Length; i++)
    {
    arrayToCopyTo.Add((Type) ArrayListToCopy);
    }
    • Post Points: 5
  • 06-20-2005 4:16 AM In reply to

    • derouyag
    • Not Ranked
    • Joined on 03-31-2005
    • Posts 2
    • Points 10

    RE: ArrayList deep copy?

    THE ULTIMATE IN COPYING/CLONING OBJECTS!!!!
     
    Create yourself a nice abstract base class, which includes the ICLONABLE interface.  Then include this code below in your base class.  Derive all of your domain objects from there.  You could even try a nice abstract base collection class and also include this code below.  Be creative, but always use this code on your base classes.  Don't forget to make them SERIALIZABLE.  This will clone datatables, arrays, custom collections, simple properties, hashtables... whatever you want.  Why add code to all of your classes when this will do it all!
     
      public object Clone(bool doDeepCopy)
      {
       if (doDeepCopy)
       {
        BinaryFormatter BF = new BinaryFormatter();
        MemoryStream memStream = new MemoryStream();
        BF.Serialize(memStream, this);
        memStream.Flush();
        memStream.Position = 0;
        return (BF.Deserialize(memStream));
       }
       else
       {
        return (this.MemberwiseClone());
       }
      }
      
      public object Clone()
      {
       return (Clone(false));
      }
     
    Gordon. jumpin  
    • Post Points: 5
  • 11-22-2005 10:26 PM In reply to

    RE: ArrayList deep copy?

    I used above successfully but then when we wrapped my solution into another project which is a COM wrapper for our project. It interfaces to a VB6 project that needs to use ours. However once launched there is no interaction until we exit.

    I am getting an error on the Deserialization. I am using the exact function above. The error is "Binary Formatter Version incompatibility. Expected version 1.0. Received version 170173..."

    Any thoughts on how to get around this? Worked great til we integrated.

    Thanks for the code and any help.

    Annmarie
    • Post Points: 5
Page 1 of 1 (9 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems