1

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

1 Answers1

2

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
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • Using that method, it tells me the array is fixed or temporarily locked –  Jun 16 '14 at 20:14
  • Followup question if you're not busy :) http://stackoverflow.com/questions/24251725/type-mismatch-when-trying-to-duplicate-an-array –  Jun 16 '14 at 20:30