|
-
Aug 18th, 2005, 10:49 PM
#1
Thread Starter
PowerPoster
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.
-
Aug 18th, 2005, 10:50 PM
#2
Re: Quick Array Question
i don't think there is. im not sure though
-
Aug 18th, 2005, 10:50 PM
#3
Re: Quick Array Question
but perhaps a variant variable (holds an array aw well) is good 
like
VB Code:
Dim myArr(4) As String
Dim myVar As Variant
'initialize myArr or whatever
myVar = myArr
'try this one if ok :D
Msgbox CStr(UBound(myVar))
-
Aug 18th, 2005, 10:56 PM
#4
Re: Quick Array Question
Try this:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim t() As Integer, y() As Integer
Dim x As Integer, str$
ReDim t(4) As Integer
For x = 0 To 3
t(x) = x
Next x
y() = t()
For x = 0 To 3
str = str & y(x)
Next x
MsgBox str
End Sub
-
Aug 18th, 2005, 10:59 PM
#5
Re: Quick Array Question
Without a loop...
VB Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
Dim Arr1(100) As Long
Dim Arr2() As Long
ReDim Arr2(UBound(Arr1))
CopyMemory Arr2(0), Arr1(0), Len(Arr1(0)) * (UBound(Arr1) + 1)
End Sub
via CVMicheal
-
Aug 18th, 2005, 11:07 PM
#6
Re: Quick Array Question
Mine copies the array without a loop. The loops just load and display data.
-
Aug 19th, 2005, 12:05 AM
#7
Thread Starter
PowerPoster
Re: Quick Array Question
Thanks All. Got it working the way I want
-
Aug 19th, 2005, 12:09 AM
#8
Re: Quick Array Question
Darn, the VBForums sever was down when I had this code to post:
VB Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Copy_Array_Long(Destination() As Long, Source() As Long, Length As Long)
CopyMemory Destination(0), Source(0), Length
End Sub
Private Sub Form_Activate()
Dim A(3) As Long, B(3) As Long
A(0) = 10: A(1) = 20: A(2) = 30
Copy_Array_Long B(), A(), Len(A(0)) * UBound(A)
Print B(0), B(1), B(2)
End Sub
-
Aug 19th, 2005, 01:16 AM
#9
Re: Quick Array Question
Slightly more versatile version of Jake's function
VB Code:
Private Sub CopyLongArray( _
ByRef psaDestination() As Long, _
ByRef psaSource() As Long _
)
CopyMemory psaDestination(LBound(psaSource)), _
psaSource(UBound(psaSource)), _
(UBound(psaSource) - LBound(psaSource)) * LenB(psaSource(LBound(psaSource)))
End Sub
-
Aug 19th, 2005, 01:38 AM
#10
Thread Starter
PowerPoster
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)
-
Aug 19th, 2005, 01:42 AM
#11
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:
Dim lArrSrc() As Long
Dim lArrDest() As Long
ReDim lArrSrc(6,10)
' later
lArrDest = lArrSrc
It only works with dynamic arrays.
-
Aug 19th, 2005, 01:56 AM
#12
Thread Starter
PowerPoster
Re: Quick Array Question * Re-opened *
Thanks penagate...that's what I was after.
-
Aug 19th, 2005, 01:59 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|