Solved

(25 pts) Copying Array to Array

Posted on 2000-04-07
5
139 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now