Visual Basic scripting dynamic array

后端 未结 1 1024
再見小時候
再見小時候 2020-12-01 22:47

So i have a vb script that sweeps through RAP (Run advertised programs) and if the program has no last run time, but that program\'s full name into an array, then i have thi

相关标签:
1条回答
  • 2020-12-01 23:24

    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
    
    0 讨论(0)
提交回复
热议问题