Results 1 to 7 of 7

Thread: Cloning Objects

  1. #1
    Member
    Join Date
    Jan 06
    Location
    Perth, Australia
    Posts
    55

    Question 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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,636

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  3. #3
    Moderator
    Join Date
    Jan 05
    Location
    Sydney
    Posts
    13,612

    Re: Cloning Objects

    As a rule of thumb, stuff that's serialisable is generally cloneable, and vice versa.

  4. #4
    Member
    Join Date
    Jan 06
    Location
    Perth, Australia
    Posts
    55

    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?

  5. #5
    Noodly Appendage wossname's Avatar
    Join Date
    Aug 02
    Location
    #!/bin/bash
    Posts
    5,645

    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.

  6. #6
    Special Guest - Microsoft
    Join Date
    Dec 07
    Posts
    3

    Re: Cloning Objects

    Quote 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.

  7. #7
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    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
  •