In order to investigate the various methods of obtaining a copy of an array, I wrote a short program (the VS 2003 source code can be downloaded from the link) and systematically worked my way through copying or cloning both System and User Designed Types.
These tests also introduce the concept of a ‘deep copy’, which means that a copy of any Object Type must be a completely independent copy of that Object. Everything directly or indirectly referenced by any field in that Object must also be copied.
A true deep copy was thought, by me at least, to be difficult to attain as it would have to be implemented on an object-by object basis, and only then so long as each object’s Type implemented the IClonable interface. However, Richter & Balena suggested a remedy using Streams. Balena enhanced the method in "Programming Microsoft Visual Basic .NET Version 2003" (MS Press 2004, ISBN 0-7356-2059-8) and is substantially used in the following tests as 'CloneObject'. The catch, of course, is that the objects in the Array being deep copied by this technique must each support Serialisation, as must each Object referred to by them.
The following tables list the tests that were carried out on arrays of either Type in order to establish the effectiveness of producing a truly independent copy of that array.
The test of System Types is implemented by creating Array1() and filling it with appropriate data. Array1() is then copied to Array2() using the selected method.
Array1() is altered using the method shown in Table 1 and the arrays are then compared for independence.
A list of the data used are shown in Appendix 1.
The Person Class, which supports both Clone and CloneObject methods, was subclassed into PetOwner and Team Classes.
The PetOwner Object can have a reference to a Pet Object, which does not have a Clone Method, or a SecondPet Object, which does.
Team Objects have no references.
Array1() is filled with one of the Person Objects, see Appendix 1. Array1() is copied to Array2() using the chosen method, and then altered by changing Person, Pet or Team data as detailed in Appendix 1. The arrays are then compared.
Results and Conclusion
The results of the test are tabulated below:
Testing for Existance of Second Array
There is a facility to compare pointers to see whether they point to the same Object on the Managed Heap, Array1() is Array2().
I have used this comparison to provide the text for the ‘Independence’ Label on the lower right corner of the Form in the accompanying program. The test takes place after the data is written for the second time.
Whilst it provides accurate results for the System Types it cannot serve for the User Designed Types as (although the Pointers might not be to the same Object) the content of those Arrays is, in some cases, not the required independent copy (see Table 4 with Table 5).
This has been a most interesting exercise and in doing the research I have learned some valuable lessons about how .NET 1.1 organises variables in memory. In particular, I now know what to avoid!
As can be easily seen, the copying of an array of System Types if straightforward, but making a straight copy of the Array pointer to another variable should be avoided, unless a ‘second’ copy is required of course.
User Designed Types are also straightforward so long as there is no type referred to within the Object, a little more complicated if there is, and impossible to do by a Native Framework Method if the Object referred to does not, itself, support cloning.
However, one thing that stands out very clearly indeed is that any copy involving the CloneObject Method/Function will succeed no matter what, provided that all of the objects involved are serialisable.
All of the tests were carried out using the associated program (developed using VS2003) which can be downloaded, with the source code, from the link. Please feel free to explore different options and to change the code as you see fit. I should, however, like to hear of your experiences so that I can keep this paper up-to-date.
No doubt there will be questions and comments, which I will be happy to deal with on-line.
Acknowlegements and Appendix
I could not have started upon this quest without having (frequently) to hand a copy of ‘Building Applications and Components with VB.NET’ by Pattison & Hummel published by Addison-Wesley ISBN 0-201-73495-8, from which Figure 1 is based and also from which I learned so much. I should also mention Dr Hummel’s webcasts to be found on msdn.microsoft.com which are without equal (IMHO).
Secondly, ‘Applied Microsoft .NET Framework Programming in MS Visual Basic .NET’ by Richter & Balena published by Microsoft Press ISBN 0-7356-1787-2, particularly chapter 14.
Francesco Balena’s book ‘Programming Microsoft Visual Basic .NET (2004) has already been mentioned.