Results 1 to 13 of 13

Thread: Quick Array Question

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved Quick Array Question

    Is there a way to copy the contents of one array to another without using a loop?

    eg. MyArr1() = MyArr2()
    Last edited by lintz; Aug 19th, 2005 at 01:57 AM.

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Quick Array Question

    i don't think there is. im not sure though

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Quick Array Question

    but perhaps a variant variable (holds an array aw well) is good

    like
    VB Code:
    1. Dim myArr(4) As String
    2. Dim myVar As Variant
    3.  
    4.  
    5. 'initialize myArr or whatever
    6.  
    7. myVar = myArr
    8.  
    9. 'try this one if ok :D
    10. Msgbox CStr(UBound(myVar))

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Quick Array Question

    Try this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim t() As Integer, y() As Integer
    5.   Dim x As Integer, str$
    6.   ReDim t(4) As Integer
    7.   For x = 0 To 3
    8.     t(x) = x
    9.   Next x
    10.   y() = t()
    11.   For x = 0 To 3
    12.     str = str & y(x)
    13.   Next x
    14.   MsgBox str
    15. End Sub

  5. #5
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Quick Array Question

    Without a loop...
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    4.  
    5. Private Sub Form_Load()
    6.     Dim Arr1(100) As Long
    7.     Dim Arr2() As Long
    8.         ReDim Arr2(UBound(Arr1))
    9.     CopyMemory Arr2(0), Arr1(0), Len(Arr1(0)) * (UBound(Arr1) + 1)
    10. End Sub

    via CVMicheal

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Quick Array Question

    Mine copies the array without a loop. The loops just load and display data.

  7. #7

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Quick Array Question

    Thanks All. Got it working the way I want

  8. #8
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Quick Array Question

    Darn, the VBForums sever was down when I had this code to post:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    4.  
    5. Private Sub Copy_Array_Long(Destination() As Long, Source() As Long, Length As Long)
    6.    
    7.     CopyMemory Destination(0), Source(0), Length
    8.  
    9. End Sub
    10.  
    11. Private Sub Form_Activate()
    12.  
    13.     Dim A(3) As Long, B(3) As Long
    14.    
    15.     A(0) = 10: A(1) = 20: A(2) = 30
    16.    
    17.     Copy_Array_Long B(), A(), Len(A(0)) * UBound(A)
    18.    
    19.     Print B(0), B(1), B(2)
    20.  
    21. End Sub

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Quick Array Question

    Slightly more versatile version of Jake's function
    VB Code:
    1. Private Sub CopyLongArray( _
    2.     ByRef psaDestination() As Long, _
    3.     ByRef psaSource() As Long _
    4. )
    5.     CopyMemory psaDestination(LBound(psaSource)), _
    6.                psaSource(UBound(psaSource)), _
    7.                (UBound(psaSource) - LBound(psaSource)) * LenB(psaSource(LBound(psaSource)))
    8. End Sub

  10. #10

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Quick Array Question * Re-opened *

    I'm using the code submitted by |2eM!x but my question is can I use that code for a 2D Array?

    eg MyArr(6,10)

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Quick Array Question * Re-opened *

    Offhand I don't know how 2D arrays are stored in memory so I couldn't help you with CopyMemory there.

    If you declare the arrays as dynamic and subsequently ReDim the source array to the dimensions you want then you can just equate it with the destination array.

    e.g.
    VB Code:
    1. Dim lArrSrc() As Long
    2. Dim lArrDest() As Long
    3.  
    4. ReDim lArrSrc(6,10)
    5.  
    6. ' later
    7. lArrDest = lArrSrc

    It only works with dynamic arrays.

  12. #12

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Quick Array Question * Re-opened *

    Thanks penagate...that's what I was after.

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Quick Array Question

    No worries

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width