在下面的代码中
For i = LBound(arr) To UBound(arr)
使用LBound
请问有什么意义? 当然,这总是0。
为什么不使用For Each
? 这样你就不需要关心LBound
和UBound
是什么了。
Dim x, y, z
x = Array(1, 2, 3)
For Each y In x
z = DoSomethingWith(y)
Next
有一个很好的理由不使用For i = LBound(arr) To UBound(arr)
dim arr(10)
分配数组的11个成员,0到10(假设VB6默认为Option Base)。
许多VB6程序员假设该数组是基于一个的,并且从不使用分配的arr(0)
。 我们可以通过使用For i = 1 To UBound(arr)
或For i = 0 To UBound(arr)
来消除潜在的错误来源,因为很清楚是否正在使用arr(0)
。
For each
数组元素,而不是指针。
这有两个问题。
当我们尝试为数组元素赋值时,它不会反映在原始元素上。 此代码将值47赋给变量i
,但不影响arr
的元素。
arr =数组(3,4,8)\n 对于每个我在arr\n 我= 47\n 下一个我\n Response.Write arr(0)' - 返回3,而不是47
我们不知道一个数组元素的索引中for each
,我们不能保证元素的顺序(尽管它似乎是为了。)
LBound
可能并不总是0。
虽然不可能在VBScript中创建除0下限以外的任何数组的数组,但仍然可以从COM组件中检索可能已指定不同LBound
的变体数组。
这就是说我从来没有遇到过这样做过的人。
可能它来自VB6。 因为在VB6中使用Option Base语句,您可以改变数组的下限,如下所示:
Option Base 1
同样在VB6中,您可以更改特定数组的下限,如下所示:
Dim myArray(4 To 42) As String
我总是用For Each ...
这是我的方法:
dim arrFormaA(15)
arrFormaA( 0 ) = "formaA_01.txt"
arrFormaA( 1 ) = "formaA_02.txt"
arrFormaA( 2 ) = "formaA_03.txt"
arrFormaA( 3 ) = "formaA_04.txt"
arrFormaA( 4 ) = "formaA_05.txt"
arrFormaA( 5 ) = "formaA_06.txt"
arrFormaA( 6 ) = "formaA_07.txt"
arrFormaA( 7 ) = "formaA_08.txt"
arrFormaA( 8 ) = "formaA_09.txt"
arrFormaA( 9 ) = "formaA_10.txt"
arrFormaA( 10 ) = "formaA_11.txt"
arrFormaA( 11 ) = "formaA_12.txt"
arrFormaA( 12 ) = "formaA_13.txt"
arrFormaA( 13 ) = "formaA_14.txt"
arrFormaA( 14 ) = "formaA_15.txt"
Wscript.echo(UBound(arrFormaA))
''displays "15"
For i = 0 To UBound(arrFormaA)-1
Wscript.echo(arrFormaA(i))
Next
希望能帮助到你。