Visual Basic scripting dynamic array

You can’t re-dimension a fixed-size array (Dim vprglist(10)). If you want a dynamic array, define a “normal” variable and assign an empty array to it:

Dim vprglist : vprglist = Array()

or define it directly with ReDim:

ReDim vprglist(-1)

Then you can re-dimension the array like this:

If vprogram.LastRunTime = "" Then
  ReDim Preserve vprglist(UBound(vprglist)+1)
  vprglist(UBound(vprglist)) = vprogram.FullName
  i = i + 1
End If

ReDim Preserve will copy all elements of the array into a new array, though, so it won’t perform too well in the large scale. If performance is an issue, you’d better use the System.Collections.ArrayList class instead:

Dim vprglist : Set vprglist = CreateObject("System.Collections.ArrayList")
...
If vprogram.LastRunTime = "" Then
  vprglist.Add vprogram.FullName
  i = i + 1
End If

Leave a Comment