9

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

1 Answer 1

16

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.