Create a copy of an Object-VBForums
Results 1 to 3 of 3

Thread: Create a copy of an Object

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002

    Create a copy of an Object


    I want to create an exact copy (clone) of a class instance ( not reference). This original class also contains instances of other classes.

    I looked on this forum and it has been said there is no way to do this.

    Just want to make sure it is not possible to create an exact copy of an existing class instance?


  2. #2
    Fanatic Member
    Join Date
    Feb 2017

    Re: Create a copy of an Object

    There is no way to replicate objects globally, without knowing the intrinsic object logic.

    Suppose that the object has two properties variables with references to other objects:

    Public MainForm as Form
    Public WordInstance as Object

    What should the replicator do?
    1) Load a new instance of MainForm and open another WordInstance?
    2) Just set a new reference to the same MainForm and WordInstance objects already opened?

    There is no way to know.

    So, the usual way is that each object needs to provide a Clone method, and replicate itself:

    Public Function Clone() as MyObjectType
    Dim VarMyObjectType as MyObjectType

    Set VarMyObjectType = New MyObjectType

    ' copy all the variables properly
    ' If the object has other objects inside, they also must provide a Clone method

    Set Clone = VarMyObjectType
    End Sub

  3. #3
    Fanatic Member
    Join Date
    Sep 2012

    Re: Create a copy of an Object

    Agree with Eduardo. You can only write Copy/Clone functions manually.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width