I'm trying to create a dynamic array to later use to get the median of several number values. I have the following:
Dim DaysArray(), i, val1, val2
Sub BuildArray
i = 0
...
While Not recordset.EOF
ReDim Preserve DaysArray(i)
DaysArray(i) = recordset(value)
i = i + 1
Wend
...
End Sub
Sub ShowArrayValue
...
Array.Sort(DaysArray)
If DaysArray.Length Mod 2 <> 0 Then
Response.Write DaysArray(DaysArray.GetUpperBound(0)\2)
Else
val1 = DaysArray(DaysArray.Length\2)
val2 = DaysArray((DaysArray.Length\2) - 1)
Response.Write (val1 + val2)\2
End If
...
End Sub
However, during the first iteration thru the loop I get "Object required: Array" on the Array.Sort(DaysArray) line?
GetRowsmethod return your data as 2-dimensional array automaticallyrecordset.value, somewhere in there you are missing a reference to the field -recordset("thefieldname").Value, assuming this is anADODB.Recordsetobject reference.Dim DaysArray()"defines" an array without size and should NEVER be used anywhere (as I discussed here). If you want a dynamically sized VBScript array: useReDim DaysArray(-1)for defining an empty dynamic array, and then resize it in your loop e.g. like this:ReDim Preserve DaysArray(UBound(DaysArray)+1). Beware of the inherent performance issues of this approach.