user2074102
user2074102

Reputation:

vbscript won't return multidimensional array

This is really getting annoying. The following is a method which is contained in a custom database class. I query data into a recordset then try to convert this data into an array w/o field names. It appears to be working within the function because I setup a response.write to check if there were any values. But once out of the function, things go haywire (the array is not the same).

Public Function To2Array()
    dim A, x, columns
    columns = Rs.Fields.Count
    Rs.MoveFirst()
    x = 0
    do until Rs.EOF
        Redim A(x+1,columns)
        for y = 0 to columns - 1
            A(x,y) = Rs.Fields.Item(y)
                            response.write A(x,y) 'returns correct value
        Next
        x = x + 1       
        Rs.MoveNext()
    loop
    To2Array = A
End Function 

I assign the return array but there appears to be nothing.

arr = db.To2Array()
response.write(arr(1,0)) 'returns nothing

Can anyone figure out what I'm doing wrong?

Upvotes: 0

Views: 736

Answers (2)

Riv
Riv

Reputation: 1859

You lose the values in A every time you redim it. Using redim preserve prevents this, but you can only redim the last array dimension when you use preserve.

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

  1. You can only grow the last dimension of an VBScript array. So you need an colsXrows array.
  2. To keep the 'old' part of a dynamic array, you must ReDim Preserve.
  3. To get a two dimensional array from a recordset, use .GetRows - and avoid all risks of 'rolling your own'.

Upvotes: 3

Related Questions