In the code below
For i = LBound(arr) To UBound(arr)
What is the point in asking using LBound
? Surely that is always 0.
original title: "What is the best way to iterate through an array in Classic Asp VBScript?"
In the code below
For i = LBound(arr) To UBound(arr)
What is the point in asking using LBound
? Surely that is always 0.
नीचे दिए गए कोड में I के लिए = एलबाउंड (गिरफ्तार) टू यूबाउंड (गिरफ्तारी) LBound का उपयोग करने के लिए कहने में क्या मतलब है? निश्चित रूप से वह हमेशा 0 है।
यह अनुवाद के बाद का सारांश है, अगर आपको पूरा अनुवाद देखने की आवश्यकता है, तो कृपया 'अनुवाद' आइकन पर क्लिक करें
Why not use
For Each
? That way you don't need to care what theLBound
andUBound
are.There is a good reason to NOT USE
For i = LBound(arr) To UBound(arr)
dim arr(10)
allocates eleven members of the array, 0 through 10 (assuming the VB6 default Option Base).Many VB6 programmers assume the array is one-based, and never use the allocated
arr(0)
. We can remove a potential source of bugs by usingFor i = 1 To UBound(arr)
orFor i = 0 To UBound(arr)
, because then it is clear whetherarr(0)
is being used.For each
makes a copy of each array element, rather than a pointer.This has two problems.
When we try to assign a value to an array element, it doesn't reflect on original. This code assigns a value of 47 to the variable
i
, but does not affect the elements ofarr
.We don't know the index of an array element in a
for each
, and we are not guaranteed the sequence of elements (although it seems to be in order.)LBound
may not always be 0.Whilst it is not possible to create an array that has anything other than a 0 Lower bound in VBScript, it is still possible to retrieve an array of variants from a COM component which may have specified a different
LBound
.That said I've never come across one that has done anything like that.
Probably it comes from VB6. Because with Option Base statement in VB6, you can alter the lower bound of arrays like this:
Also in VB6, you can alter the lower bound of a specific array like this:
I've always used For Each...
This is my approach:
Hope it helps.