Solved

(25 pts) Copying Array to Array

Posted on 2000-04-07
5
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
to transfer string from C lanaguage to VBA 4 85
MS SQL store procedure to calculate and return result 6 74
Child Form in front 4 66
Export Data to Different .csv Files 27 133
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…
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…
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…

739 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