Results 1 to 4 of 4

Thread: Create a copy of an Object

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2002
    Posts
    481

    Create a copy of an Object

    Hello,

    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?

    thanks

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    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
    PowerPoster
    Join Date
    Sep 2012
    Posts
    2,083

    Re: Create a copy of an Object

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

  4. #4
    Junior Member
    Join Date
    Oct 2021
    Posts
    25

    Re: Create a copy of an Object

    I just posted a way, not sure it would suit the nesting, however, there is a scheme to memory address by pointers for objects called VTable, that you could possibly go through and check for global flags and handles. This example might be XP only, the online API reference says XP minimum for clients and Server 2003 for server.

    https://www.vbforums.com/showthread....ects-In-Memory

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