我试图用AD组中的成员填充数组。尝试将newArray(count)设置为用户名时,我一直收到以下错误。
Microsoft VBScript runtime error: Subscript out of range
以下是相关代码:
'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
....
答案 0 :(得分:2)
你的
Dim newArray()
创造了一个可憎的:一个无法生长的数组,因为UBound失败了:
>> Dim aBomination()
>> ub = UBound(aBomination)
>>
Error Number: 9
Error Description: Subscript out of range
创建一个在运行时确定大小的动态数组的正确方法(例如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