We help IT Professionals succeed at work.

Array to array

folletxavier
folletxavier asked
on
Is there a fast(no loops) way to copy the content of an array to another array?
I actually need to copy the value of the fields 5 to 9 from a first array( dim myFirstArr(0 to 9) ) to a second array (dim mySecondArr(0 to 4)).

Thanks.
/Xavier
Comment
Watch Question

Commented:
It depends on what the array contains. Under certain circumstances, it's possible to use rtlmovememory to copy the array contents at a low level. You'll have problems doing this however if the array contains objects or (I think, though I'm not 100% sure) variable length strings.

Author

Commented:
>>It depends on what the array contains
My arrays are byte arrays.
Do you have some code demonstrating the use of rtlmovememory?
/Xavier
Commented:
Something like this..

  Call CopyMem(mySecondArr(0), myFirstArray(5), 5)

Make sure to define the API declaration in the Form Declaration part (change the Public to Private) or in a module (Public)...

  Public Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Commented:
For the above to work make sure you define the arrays with code like "Dim myFirstArr(0 to 9) As Byte" and "Dim mySecondArr(0 to 4) As Byte".

Author

Commented:
I actually had time to find out about it myself but your code is exactly what I found so full points for you.
Thanks
/Xavier

Commented:
VBMaster - you got there first!

Author

Commented:
A problem with EE is that only one can get the grade and the points... This results in unfair question validation. I was forced to give points to 1 of you guys though you worked on it both.
Sorry
/Xavier

Commented:
No worries. It sometimes adds to the fun - trying to type an answer out as quickly as possible before somebody else gets in :)

Author

Commented:
Cool!
Thanks again
'Till next time!
Reguards
/Xavier

Explore More ContentExplore courses, solutions, and other research materials related to this topic.