标超出范围,同时将项目添加到阵列 [英] subscript out of range while adding items to array

查看:191
本文介绍了标超出范围,同时将项目添加到阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以填补从AD组成员的数组。我不断收到以下错误试图newArray(计数)设置为用户名。

  Microsoft VBScript运行时错误:下标越界

下面是相关code:

 设置域变量和东西了,验证工作
昏暗newArray()
昏暗的点¯x
X = 0难道能够在X< 1
    设置objGroup = GetObject的(WINNT://&放大器; strDomain&安培;/&放大器; strGroup&安培;组)
    数= 0
    对于每个objUser在objGroup.Members
        newArray(计数)= objUser.FullName
        数=计+ 1
    下一个
....


解决方案

 暗淡newArray()

创建一个憎恶没有不能生长大小的数组,因为UBound函数失败:

 >>昏暗憎恶()
>> UB = UBound函数(憎恶)
>>
错误号:9
错误描述:下标越界

正确的方法来创建在运行时确定的大小动态数组(如17,它cound为-1,如果你想没有elments数组开始)和 - 如果需要的话 - 后来成长,它是:

 >>使用ReDim。一个好的(17)
>> UB = UBound函数(。一个好的)
>> WScript.Echo UB
>>使用ReDim。一个好的(UBound函数(。一个好的)+1)
>> 。一个好的(UBound函数(。一个好的))=尾巴
>> WScript.Echo UBound函数(。一个好的)。一个好的(UBound函数(。一个好的))
>>
17
18尾

I'm trying to fill an array with members from an AD group. I keep getting the following error while trying to set newArray(count) to the user name.

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:

>> 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 (e.g. 17, it cound be -1 if you want to start with an array of no elments) and - if need be - grow it later is:

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

这篇关于标超出范围,同时将项目添加到阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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