How to copy an array to another array

  • Follow


I need to copy the content of a 2D array to another identical 2D array. I 
currently do it this way...

for X=1 to 1000
   for Y = 1 to 1000
      A(X,Y) = B(X,Y)
   next Y
next X

Is there a better/quicker way to do it?
-- 
Stephenc
0
Reply Utf 4/7/2010 4:14:01 PM

stephenc wrote:
> I need to copy the content of a 2D array to another identical 2D array. I 
> currently do it this way...
>
> for X=1 to 1000
>    for Y = 1 to 1000
>       A(X,Y) = B(X,Y)
>    next Y
> next X
>
> Is there a better/quicker way to do it?

There are quicker ways, yes.  But I'd be hard pressed to say they're 
"better" unless you're optimizing a *serious* bottleneck.

I have a SafeArray class on my site that you can use to represent the 
SAFEARRAY structure of any native array.  You'd have to download that 
(from http://vb.mvps.org/samples/MapFile), and use code something like 
this (indented to highlight wordwrap):

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

   Public Sub test()
      Dim a() As Long
      Dim b() As Long
      Dim i As Long

      Dim saA As CSafeArray
      Set saA = New CSafeArray

      Dim saB As CSafeArray
      Set saB = New CSafeArray

      ReDim a(1 To 100)
      For i = LBound(a) To UBound(a)
         a(i) = i
      Next i

      With saA
         .AttachToArray a
         ReDim b(.LowerBound To .LowerBound + .CountOfElements)
         saB.AttachToArray b
         Call CopyMemory(ByVal saB.PointerToData, _
                         ByVal .PointerToData, _
                         .SizeofElements * .CountOfElements)
      End With

      saA.ReleaseArray
      saB.ReleaseArray
   End Sub

Also, in CSafeArray, you'd want to redeclare the VarPtrArray function, 
so that it works in VBA.  This seems to do that, when I test here:

   Private Declare Function VarPtrArray Lib "vbe6.dll" Alias "VarPtr" 
(Var() As Any) As Long

Obviously, this is an *incredible* amount of work to go to, unless 
you're saving some serious cycles in the process.  Which is totally 
possible!  But if all you're trying to save is a bit of typing, well, 
you'll want to reconsider more than a few things.

Hope this helps...   Karl

-- 
..NET: It's About Trust!
http://vfred.mvps.org


0
Reply Karl 4/7/2010 8:22:58 PM

Karl E. Peterson wrote:
> I have a SafeArray class on my site that you can use to represent the 
> SAFEARRAY structure of any native array.  

More background on that class:

Black Belt Programming, July 2000: Modify a Variable's Pointer
http://vb.mvps.org/articles/bb200007.asp

-- 
..NET: It's About Trust!
http://vfred.mvps.org


0
Reply Karl 4/7/2010 8:35:04 PM

A = B

-- 
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"stephenc" <nospam> wrote in message 
news:6FB2F8DC-980B-491D-A72E-75CD517BD59A@microsoft.com...
> I need to copy the content of a 2D array to another identical 2D array. I
> currently do it this way...
>
> for X=1 to 1000
>   for Y = 1 to 1000
>      A(X,Y) = B(X,Y)
>   next Y
> next X
>
> Is there a better/quicker way to do it?
> -- 
> Stephenc 

0
Reply Doug 4/8/2010 1:42:54 AM

Doug Robbins - Word MVP wrote:
> A = B

Well sure, that'll work in ~some~ cases...  <g>

-- 
..NET: It's About Trust!
http://vfred.mvps.org


0
Reply Karl 4/8/2010 1:58:46 AM

Thanks Karl and Doug,
Sorry I didn't reply sooner but I thought I'd posted the question to the 
Excel programming forum, and that the posting had failed. So, it was here in 
the Word programming forum all the time. Doh!
Your replies answered my question exactly, so I've learned something useful 
today. Thanks again for your help!
-- 
Stephenc


"Karl E. Peterson" wrote:

> Karl E. Peterson wrote:
> > I have a SafeArray class on my site that you can use to represent the 
> > SAFEARRAY structure of any native array.  
> 
> More background on that class:
> 
> Black Belt Programming, July 2000: Modify a Variable's Pointer
> http://vb.mvps.org/articles/bb200007.asp
> 
> -- 
> ..NET: It's About Trust!
> http://vfred.mvps.org
> 
> 
> .
> 
0
Reply Utf 4/26/2010 10:11:01 AM

5 Replies
4741 Views

(page loaded in 0.226 seconds)

10/18/2014 10:24:49 AM


Reply: