Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing arrays of unknown size!

Status
Not open for further replies.
Nov 9, 2001
4
0
0
GB
I have a small app that at some point sets up an array of doubles of x dimensions of size l_x. x can be up to 20, and the size of each dimension can be anything.
What is the quickest way of setting each element in the array to a certain number (say 1), while not knowing the number of dimensions? I think the "
Code:
for each...Next
" loop may be the one, but I can't find a decent reference for using it for multi-dimensional arrays rather than 1-d arrays or collections of standard objects.
Many thanks for nay help,

Andy.
 

Hi, this should work:
----------------------------------------
Dim i as long, j as long

for i = 0 to ubound(TheArray)
for j = 0 to ubound(TheArray,2)
TheArray(i,j) =1
next j
next i
----------------------------------------
For an n by m array (which is how 2D arrays in VB comes).

It is, however also possible to make arrays of arrays, in which case each 'row' does not necessarily have the same length. Is that your situation?

And finaly, the method is probably not the fastest (writing directly to the memory block that stores the array is faster), but I would take a lot of code to improve and I doubt that the improvement would be significant. Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Or:

Dim vItem As Variant

For each vItem in testarray
Debug.Print vItem
Next vItem
 
CClint,
This is the form I've seen for reading the values, but is it possible to use vItem to write back to the array?

Sunaj - cheers for the reply, but this requires that the dimensionality is known before you start (i.e. 2d, 3d, 4d etc)
 
Andy,

Did you find a solution?
Otherwise, try this. It is a little cumbersome.....

-----------------------------------------------------------
Option Explicit

Private Type SAFEARRAYBOUND
cElements As Long
lLbound As Long
End Type
Private Type SAFEARRAY60D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
Bounds(0 To 59) As SAFEARRAYBOUND
End Type
Private Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Function ResetDblArray(ByVal ArrPtr As Long) As Long
Dim sa As SAFEARRAY60D, saPtr As Long, d As Double
Dim i As Long, numEls As Long, cDims As Integer
CopyMemory saPtr, ByVal ArrPtr, 4
CopyMemory cDims, ByVal saPtr, Len(cDims)
CopyMemory sa, ByVal saPtr, Len(sa) - 8 * (60 - cDims)
numEls = 1
For i = 0 To sa.cDims - 1
numEls = numEls * sa.Bounds(i).cElements
Next
d = 1
For i = 0 To numEls - 1
CopyMemory ByVal sa.pvData + i * 8, d, LenB(d)
Next i
End Function

Private Sub Command1_Click()
Dim a() As Double
ReDim a(1, 5, 3, 7, 8)
ResetDblArray (VarPtrArray(a))
MsgBox a(1, 3, 2, 6, 4)
End Sub
------------------------------------------------------------ Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top