Results 1 to 8 of 8

Thread: How to copy an object?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    How to copy an object?

    Code:
    Dim a As Object
    Dim b As Object
    Set b = a
    Set b = a will not copy a to b. It only changes the reference.




    How do I copy objects just like in C++?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to copy an object?

    You have to copy it manually. Setting A=B, only makes A & B reference the same object. So, depending on what Object is, perform necessary action to reproduce it. For example, if it were a picture, use LoadPicture() to load the image into a different object. If it were a custom class you created, you should build a function that copies itself to a new passed object, initialized with the keyword New: Set [variable] = New [Object]
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: How to copy an object?

    Quote Originally Posted by LaVolpe
    You have to copy it manually. Setting A=B, only makes A & B reference the same object. So, depending on what Object is, perform necessary action to reproduce it. For example, if it were a picture, use LoadPicture() to load the image into a different object. If it were a custom class you created, you should build a function that copies itself to a new passed object, initialized with the keyword New: Set [variable] = New [Object]
    wow..
    ok

    my object here is collection (of a thousand elements).

    so the only way is to use for each statement to copy the elements?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to copy an object?

    More or less. But why would you need to copy it? And based on that answer does the entire collection need to be copied? Maybe explaining the scenario might lead to alternate solutions.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: How to copy an object?

    Quote Originally Posted by LaVolpe
    More or less. But why would you need to copy it? And based on that answer does the entire collection need to be copied? Maybe explaining the scenario might lead to alternate solutions.
    Code:
    Dim a As New Collection
    Dim i As Integer
    
    a.Add 4
    a.Add 5
    a.Add 6
    
    i = my_function(a)
    
    
    
    Private Function my_function(ByVal x As Collection) As Integer
    'This function makes some undersired changes to the original collection and returns a value
    'The original collection needs to be reserved
    'Despite Byval statement, the object is passed by reference
    
    Dim h As New Collection
    Set h = x      'still fails
    
    
    End Function
    Last edited by winterslam; Nov 14th, 2008 at 09:58 AM.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to copy an object?

    Is your collection only going to contain single numbers? If so, use an array instead of a collection and you can copy that easily enough.

    Regarding your error: h.Add a.Item(x)
    or use a For Each loop, but you'll still need h.Add
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: How to copy an object?

    Quote Originally Posted by LaVolpe
    Is your collection only going to contain single numbers? If so, use an array instead of a collection and you can copy that easily enough.
    Nope,
    the collection stores objects (defined by class module)

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to copy an object?

    In that case, sounds like creating your "Clone" function withn the class may be the solution. Now is the entire collection modified? If not, don't clone the entire collection, only clone the ones that will be modified.
    A rather simple clone function looks like this
    Code:
    Public Sub CloneMe(toObject As Class1) ' change class name
       Set toObject=New Class1
       ' now populate all the properties
       toObject.PropertyName = Me.PropertyName
       ' etc, etc
    End Sub
    Of course if the classes themselves store objects, then things get a bit more complicated.

    Additionally, what gets changed that needs to be preserved, maybe just making a copy of those specific things will do the trick.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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