Solved

(25 pts) Copying Array to Array

Posted on 2000-04-07
5
148 Views
Last Modified: 2010-05-02
In Visual Basic 5 / 6, if you have the array MyArray(256), and you have NewArray(256), you can't copy the contents of the first array to the second with code like this :

NewArray = MyArray

It gives an error saying can't assign to array.  I could loop through and copy each index of MyArray(256) to NewArray(256), but that's very inefficient and there's got to be a built in way to copy entire arrays to another array.

Can anyone help?  I need sample code please.

HATCHET
0
Comment
Question by:HATCHET
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2694443
it cant assign fixed arrays to eachother but dynamic arrays you can.  Try declaring them as MyArray() As Variant and NewArray() As Variant
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2694484
Azra:
In VB6 you can pass any type of an array to another, as long as the same Datatypes are used and only the Target (receiving) Array has to be defined as being dynamic.. <smile>.

Dim MyArray1() as String
Dim MyArray2(80) as String

MyArray1() = MyArray2()
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2694502
yes but I didnt see any reason why he would have one of each, just thought itd be simplest to throw them both in as dynamic.  but then, i guess we know what happens when we assume...
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 25 total points
ID: 2694685
You can use CopyMemory to copy the arrays:

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private Sub Command1_Click()
    Dim MyArray(255) As Long
    Dim NewArray(255) As Long
    Dim i As Long
    'Initialize the first array
    For i = 0 To 255
        MyArray(i) = i
    Next
    'Copy the array
    Call CopyMemory(NewArray(0), ByVal VarPtr(MyArray(0)), LenB(MyArray(0)) * (UBound(MyArray) + 1))
    'verify copy
    For i = 0 To 255
        Debug.Print NewArray(i)
    Next
End Sub
0
 
LVL 3

Author Comment

by:HATCHET
ID: 2695109
AzraSound & wsh2,

The dynamic array idea is a good one, but a little more work than I was looking for.  I think Erick37 has the best idea... at least for the application I'm after.

Thanks all for the input.

HATCHET
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question