-
Dec 12th, 2007, 12:06 AM
#1
Thread Starter
Member
Cloning Objects
Hey guys,
A while back I was looking for a way to clone certain (reference type) objects. Some classes such as the DataTable have a Clone method, but this doesn't to be the case for all classes in the Framework.
Is there a reason for this, and is it likely to change in future versions? I'm a bit of a noob, so maybe there's something I'm missing
Cheers
-
Dec 12th, 2007, 12:35 AM
#2
Re: Cloning Objects
Not everything is clonable. Sometimes it doesn't make sense to make a particular class clonable... take an integer for example. Doesn't really make sense to make that clonable. Then again, that's a simplistic view, not to mention that an integer is a value item, and not a reference item. As the developer, when you create class, it's up to you to determine if an object can be cloned, and how.
-tg
-
Dec 12th, 2007, 02:13 AM
#3
Re: Cloning Objects
As a rule of thumb, stuff that's serialisable is generally cloneable, and vice versa.
-
Dec 12th, 2007, 03:04 AM
#4
Thread Starter
Member
Re: Cloning Objects
I understand that not every class should be clonable, namely value types. But surely every reference type should be? Why just the serialisable ones?
-
Dec 12th, 2007, 07:18 AM
#5
Re: Cloning Objects
You can make deep copies of many things by using Binary Serialization to a memory stream and then deserializing it out again into another variable. Assuming the NotSerialised attribute has not been used to prevent this of course.
-
Dec 12th, 2007, 07:04 PM
#6
Special Guest - Microsoft
Re: Cloning Objects
Originally Posted by trezise
I understand that not every class should be clonable, namely value types. But surely every reference type should be?
In many cases, cloning can not be done by simply copying the content of an object because it wouldn't be the right thing to do. There is an obvious question of whether it should be a deep or a shallow copy. Also, cloning is likely to lead to sharing of resources and only author of the type has the knowledge about what would the sharing mean and how it should be handled, if supported at all. So, it is up to the author to decide whether cloning feature should be available and how exactly it is going to be implemented.
Thanks,
Aleksey Tsingauz,
VB.NET Compiler Dev Team.
-
Dec 12th, 2007, 09:18 PM
#7
Re: Cloning Objects
To take that a bit farther, there actually is a default clone:
Dim a as New <some type of class>
dim b as <same type of class>
b=a
That is a type of cloning known as a shallow copy. Wossy mentioned a rough VERY deep copy, which could have been implemented as a default clone option for most reference types.
Therefore, you have two possible "default" options. Which one is correct? One? The other? or none of the above? MS opted for the shallow copy as the default, and left it up to you to decide what level of deeper copy you would implement.
The variation on this question that I have long wondered is why there isn't a copy constructor pattern similar to that found in C++. You can sort of make one, but not really. Since many people here would be familiar with the C++ copy constructor, why not extend it into VB?
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|