Visual Basic 脚本动态数组 [英] Visual Basic scripting dynamic array

查看:49
本文介绍了Visual Basic 脚本动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 vb 脚本,它扫描 RAP(运行广告程序),如果该程序没有上次运行时间,但该程序的全名到一个数组中,那么我将这个数组回显到一个消息框.我初始化数组以存储 10 个值,但是为了保持消息框干净,我想在找到所有程序后重新调整数组大小(不应该超过 3 个,但谁知道客户端).但是,我似乎无法调整数组大小,它会打印一个消息框,其中包含 10 个数组插槽 + 它找到的程序.

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 this array echo to a message box. I intialize the array to store 10 values, however to keep the message box clean i wanted to ReDim the array size once it had found all the programs (shoudn't ever be more than 3 but who knows with clients). However i can't seem to get the array to resize and it prints a message box with 10 array slots + the program it found.

Dim vprglist(10)
Dim i  
Dim strBuf 
Dim intIndex 

Set vprograms = oUIResource.GetAvailableApplications

i = 0 
For Each vprogram In vprograms
     If vprogram.LastRunTime = "" Then
         vprglist(i) = vprogram.FullName
         i = i + 1
     End If   
Next

ReDim Preserve vprglist(i)

If vprglist <> Null Then  

    For intIndex = LBound(vprglist) To UBound(vprglist)
        strBuf = strBuf & "   -  " & vprglist(intIndex) & vbLf 
    Next
        vmsgbox = MsgBox("Do you want to Install(Yes) or Defer(No) the follow software: " & vbLf & strBuf,64+4)
        Select Case vmsgbox

推荐答案

你不能重新定义一个固定大小的数组 (Dim vprglist(10)).如果你想要一个动态数组,定义一个普通"变量并为其分配一个空数组:

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()

或者直接用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 会将数组的所有元素复制到一个新数组中,因此它在大规模上不会表现得很好.如果性能是一个问题,您最好使用 System.Collections.ArrayList 类:

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

这篇关于Visual Basic 脚本动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆