coson
coson

Reputation: 8659

VBScript create an array in function based on parameter

Good Day,

I'm creating a VBScript function to return an array. But I want to pass in a parameter for the array size.

Function CreateArray(arraySize)
    Dim someArray(arraySize)               ' EXPECTED INTEGER CONSTANT
    For i = 0 to UBound(someArray)
        someArray(i) = 5
    Next

   CreateArray = someArray
End Function

But I'm getting an error:

Expected integer constant

Is this possible to do in VBScript?

TIA,

coson

Upvotes: 9

Views: 10128

Answers (1)

Joshua Honig
Joshua Honig

Reputation: 13215

Yes. You use the Redim statement:

Function CreateArray(arraySize) 
    Dim someArray()               
    Redim someArray(arraySize)
    For i = 0 to UBound(someArray) 
        someArray(i) = 5 
    Next 
    CreateArray = someArray 
End Function

Upvotes: 16

Related Questions