简体   繁体   English

将项目添加到数组时,下标超出范围

[英]subscript out of range while adding items to array

I'm trying to fill an array with members from an AD group. 我正在尝试用AD组的成员填充数组。 I keep getting the following error while trying to set newArray(count) to the user name. 尝试将newArray(count)设置为用户名时,我一直收到以下错误。

Microsoft VBScript runtime error: Subscript out of range

Here is the relevant code: 以下是相关代码:

'set up of domain variables and stuff, verified working
Dim newArray()
Dim x
x = 0

Do While x < 1
    Set objGroup = GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")
    count = 0
    For Each objUser In objGroup.Members
        newArray(count) = objUser.FullName
        count = count + 1
    Next
....

Your 你的

Dim newArray()

creates an abomination: an array of no size that can't be grown, because UBound fails: 创建一个可憎的对象:一个无法增长的无大小数组,因为UBound失败:

>> Dim aBomination()
>> ub = UBound(aBomination)
>>
Error Number:       9
Error Description:  Subscript out of range

The correct way to create a dynamic array with a size determined at run time (eg 17, it cound be -1 if you want to start with an array of no elments) and - if need be - grow it later is: 创建具有在运行时确定的大小的动态数组的正确方法(例如17,如果要以无要素的数组开始则为-1),并且(如果需要)以后再增长:

>> ReDim aGood(17)
>> ub = UBound(aGood)
>> WScript.Echo ub
>> ReDim aGood(UBound(aGood) + 1)
>> aGood(UBound(aGood)) = "tail"
>> WScript.Echo UBound(aGood), aGood(UBound(aGood))
>>
17
18 tail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM