不进入for循环

时间:2017-10-31 20:16:54

标签: loops if-statement vbscript

我有两个数组,我需要比较它们的值。我写了一个for循环,当我尝试调试时,它根本不进入循环。

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

2 个答案:

答案 0 :(得分:3)

混合数组的大小(项数)和UBound(最后有效索引)。要遍历您需要访问的所有元素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

输出:

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

答案 1 :(得分:1)

如果您正在使用多维数组(如从CellTemplate 中检索到的内存记录集中),您可能忘记在使用{{时}显式检查数组的第二个维度1}}功能。

GetRows()

最后删除了UBound()语句,因为您不能只从单维数组切换到多维数组。如果要输出数组值,则需要指定两个维度。

同样值得注意的是,'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 将无法按预期工作,但我现在还没有进入。说实话,这种方法需要重新进行。