Solved

(25 pts) Copying Array to Array

Posted on 2000-04-07
5
151 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL store procedure to calculate and return result 6 62
vbModal 12 57
Advice in Xamarin 21 96
Copy a range from 1..n excel sheets to one destination sheet 2 55
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

808 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