Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript create an array in function based on parameter

Tags:

vbscript

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

like image 557
coson Avatar asked Mar 22 '12 01:03

coson


1 Answers

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
like image 111
Joshua Honig Avatar answered Oct 06 '22 00:10

Joshua Honig