0

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?

3
  • 1
    If the recordset is an ADODB Recordset, you could also consider using the GetRows method return your data as 2-dimensional array automatically Commented Mar 6, 2019 at 16:43
  • The example makes no sense - recordset.value, somewhere in there you are missing a reference to the field - recordset("thefieldname").Value, assuming this is an ADODB.Recordset object reference. Commented Mar 6, 2019 at 16:59
  • 1
    The code you showed should raise that error with every iteration, b/c at no point in that code do you define an actual size for the array. 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: use ReDim 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. Commented Mar 6, 2019 at 17:16

1 Answer 1

0

You need to use ReDim Statement e.g. as follows:

Dim DaysArray(), i, val1, val2

Sub BuildArray
i = 0
'...
While Not recordset.EOF
    ReDim Preserve DaysArray(i)
    DaysArray(i) = recordset.value
    recordset.MoveNext
    i = i + 1
Wend
'...
End Sub

ReDim Statement

Declares dynamic-array variables, and allocates or reallocates storage space at procedure level.

Syntax

ReDim [Preserve] varname(subscripts) [, varname(subscripts)] . . .

Arguments

  • Preserve Preserves the data in an existing array when you change the size of the last dimension.
  • varname Name of the variable; follows standard variable naming conventions.

  • subscripts Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax: upper [,upper] . . .

The lower bound of an array is always zero.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks JosefZ. That got me past that error. Now I'm getting an error "Object required: Array" on the line - Array.Sort(DaysArray) in the Sub ShowArrayValue.
@M.Hondros VBScript does not contain any native thing similar to Array Object (Windows Scripting - JScript).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.