Nov 25th, 2004, 02:31 PM
Copy constructor in VB.NET
In C++ it is possible to write copy constructors such that if you do this:
a is a created copy of b. This is simple as long as all the members of TypeT are simple types, don't allocate memory, etc.
How does copy construction work in .NET? As long as all the members of a class are simple, there should be no issues. Perhaps there are never any issues, because things like arrays, that might be handled by pointers in C++ could be sized and copied transparently during construction in .NET, but it seems to me that you could still create a class where it would be nearly impossible to simply copy everything.
My usual boring signature: Nothing
Nov 26th, 2004, 04:51 AM
Some suggestions (treat them all as separate strategies):
1. Inherit the ICloneable interface and use a=b.clone()
2. consider making your class a structure (with methods) instead (if your program allows this, it might not)
3. Use binary serialisation to make an exact duplicate of your class.
I can give you some sample code for number 3 if you want it. Its just a single function that you drop into your class. This is probably the easiest way to get an *identical* copy without any differences at all. Although it might come with some overhead if your class is complex.
PM me if you want the code.
I don't live here any more.
Click Here to Expand Forum to Full Width
Survey posted by VBForums.