Solved

(25 pts) Copying Array to Array

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Suggested Courses
Course of the Month7 days, 13 hours left to enroll

632 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