Maddy
Maddy

Reputation: 37

not going into the for loop

I have two arrays and I need to compare their values. I wrote a for loop and when I am trying to debug it's not going into the loop at all.

Dim i, value
Dim flag
i = 0
For i = 0 To UBound(groupName_LocationsTable) - 1
    flag = False
    If groupName_LocationsTable(0, i) = groupName_getLocationsAPI(0, i) Then
        flag = True
    End If
Next 
If flag Then
    Response.Write groupName_LocationsTable(i) & " not found<br />" "Pass"
Else
    Response.Write groupName_LocationsTable(i) & " not found<br />"
End If

Upvotes: 0

Views: 66

Answers (2)

user692942
user692942

Reputation: 16671

If you are using the multi-dimensional arrays (like in memory recordset retrieved from GetRows()) you have likely forgotten to explicitly check the second dimension of the array when using the UBound() function.

'Check the second dimensions upper bound.
For i = 0 To UBound(groupName_LocationsTable, 2)
    flag = False
    If groupName_LocationsTable(0,i) = groupName_getLocationsAPI(0,i) Then
        flag = True
    End If
Next

Have removed the If statement at the end because you can’t just switch from single to multi-dimensional arrays. If you are going to output the array values you would need to specify both dimensions.

It’s also worth noting your flag = True won’t work as you expect but I'm not going into that now. To be honest, the approach needs to be re-worked.

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

You mix Size (number of items) and UBound (last valid index) of an array. To loop over all elements you need to access a(UBound(a)):

Option Explicit

Dim a : a = Array("one and only")
WScript.Echo TypeName(a), UBound(a), a(0)
Dim i
For i = 0 To UBound(a)
    WScript.Echo i, a(i)
Next
WScript.Echo "now you don't"
For i = 0 To UBound(a) - 1
    WScript.Echo "UBound <> Size"
Next

output:

cscript 47043861.vbs
Variant() 0 one and only
0 one and only
now you don't

Upvotes: 3

Related Questions