|
-
Sep 13th, 2010, 03:01 PM
#1
Thread Starter
New Member
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
-
Sep 13th, 2010, 03:10 PM
#2
Member
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
-
Sep 13th, 2010, 03:14 PM
#3
Thread Starter
New Member
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
-
Sep 13th, 2010, 03:21 PM
#4
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.
-
Sep 14th, 2010, 12:55 AM
#5
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.
-
Sep 14th, 2010, 08:39 AM
#6
Member
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).
-
Sep 14th, 2010, 09:17 AM
#7
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.
-
Sep 14th, 2010, 09:27 AM
#8
Re: array copy
 Originally Posted by Merri
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|