Results 1 to 8 of 8

Thread: array copy

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    2

    array copy

    Hello,
    I need to make a copy (clone) of the 1D array.

    Dim array1() as F1Object
    Dim copyarray() as F1Object

    In my code I assigned values to the arra1

    Redim copyarray(ubound(array1))

    copyarray = array1

    The problem is:
    The copyarray changes automatically when I’m modifying the array1 because the “copyarray” is just reference to “array1”

    How can I clone array1 to copyarray ?


    Thank you

  2. #2
    Member
    Join Date
    Jul 2010
    Posts
    37

    Re: array copy

    Well the obvious way (but not necessarily the most efficient) would be to do a content copy. Such as:

    Dim Array1() as F1Object
    Dim Array2() as F1Object
    Dim Element as Integer

    'Some code here to size and populate Array1

    'Resize Array To Match The Size Of Array 1
    Redim Array2(Lbound(Array1) to Ubound(Array1))

    'Copy Contents
    For Element = Lbound(Array1) to UBound(Array1)
    Array2(Element)=Array1(Element)
    Next Element


    You could even wrap this up into a function which would take two arrays ByRef and perform the copy...so you could do the clone by a simple function call.
    Last edited by LordAshes; Sep 13th, 2010 at 03:11 PM. Reason: Sample code used Byte instead of F1Object

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Re: array copy

    Hello LordAshes

    Thank you for your reply.

    I already tried to copy content element by element, but the result is the same as copyarray = array1

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

    Re: array copy

    LordAshes, if F1Object is a object/class of some sort, that will still create an array that references the objects in the source array. Therefore, changing 1 item in Array1 will change the same/referenced item in Array2.

    In VB6, there is no simple way to clone an object. You have to create a routine that creates a new instance of the class/object, then sets the clone's properties individually from the source's properties. You can build a clone function in your F1Object...
    Code:
    ' after resizing the clone array
    Dim I As Integer
    For I = LBound(Array1) To UBound(Array1)
        Array1(I).Clone CopyArray(I)
    Next
    
    ' in the F1Object, you'd have a function/sub similar to...
    Public Sub Clone(cloneObj As Object)
        Set cloneObj = New F1Object
        cloneObj.PropertyA = Me.PropertyA
        cloneObj.PropertyB = Me.PropertyB
        ... etc
    End Sub
    If F1Object is not under your control, you may have to settle for doing each property individually in a routine within your form
    Code:
    ' within your form
    Private Sub Clone(srcObject As Object, dstObject As Object)
       Set dstObject = New F1Object
       dstObject.PropertyA = srcObject.PropertyA
       dstObject.PropertyB = srcObject.PropertyB
    End Sub
    
    ' sample call
    Dim I As Integer
    ' resize array
    For I = LBound(Array1) To UBound(Array1)
        Clone Array1(I), CopyArray(I)
    Next
    Edited: Oops, forgot the most important part & updated the code above in blue
    Last edited by LaVolpe; Sep 13th, 2010 at 04:12 PM.
    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
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: array copy

    An object of a Public Persistable class can be cloned pretty simply via a PropertyBag. However this involves enough code itself in the Persistable class that it's generally not worth the overhead of creating a separate registered ActiveX DLL to make this possible.

  6. #6
    Member
    Join Date
    Jul 2010
    Posts
    37

    Re: array copy

    Doh! Quite correct. Sorry...I wrote the example for Byte first and then I noticed the the initial code was using F1Object instead...so I just changed it from Byte to F1Object...but, as pointed out, that won't work.

    You would have to dig deeper and copy not the elements of the Object array but the actual properties themselves. I am not sure what properties the F1Object has but an example might be...

    For example,

    Code:
    Array2(Element).Top = Array1(Element).Top
    Array2(Element).Left = Array1(Element).Left
    ...
    Array2(Element).Left = Array1(Element).Text
    This, of course, makes the copy very inefficient because you basically have a O(2N) process (i.e. loop within a loop...technically the properties are not a loop but there is a number of them which will be repeated by the initial loop so it can be considered a loop within a loop).

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: array copy

    In case you can tell, what kind of content F1Object holds? I'm asking this only because in some cases small changes can make it easier to clone the data. Cloning data property by property may be overly inefficient in some cases.

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

    Re: array copy

    Quote Originally Posted by Merri View Post
    In case you can tell, what kind of content F1Object holds? I'm asking this only because in some cases small changes can make it easier to clone the data. Cloning data property by property may be overly inefficient in some cases.
    Agreed. In fact if one develops a class & also wants to include a clone method, then deep thought should be given as to how the property values are stored internally (arrays, udts, bitmasking, etc)
    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