10.9.3 For Each...Next StatementsSee Also
10.9.1 While...End While and Do...Loop Statements | 10.9.2 For...Next Statements | 10.9 Loop Statements | For Each...Next Statements (Visual Basic Language Reference) | For Each...Next Statements (Visual Basic Language Concepts)
A For Each...Next statement loops based on the elements in an expression. A For Each statement specifies a loop control variable and an enumerator expression.
The loop control variable is specified either through an identifier followed by an As clause or an expression. In the case of an identifier, the identifier defines a new local variable of the type specified in the As clause, scoped to the entire For Each loop. In the case of an expression, the expression must be classified as a variable. The enumerator expression must be classified as a value and its type must be a collection type. An implicit conversion must exist from the element type of the collection to the type of the loop control variable.
The loop control variable cannot be used by another enclosing For Each statement. A For Each statement must be closed by a matching Next statement. A Next statement without a loop control variable matches the innermost open For Each. A Next statement with one or more loop control variables will, from left to right, match the For Each loops that have the same loop control variable. If a variable matches a For Each loop that is not the most nested loop at that point, a compile-time error occurs.
A type C is said to be a collection type if it implements the interface System.Collections.IEnumerable or if all of the following are true:
C contains an accessible instance method with the signature GetEnumerator() that returns a type E.
E contains an accessible instance method with the signature MoveNext() and the return type Boolean.
E contains an accessible instance property named Current that has a getter. The type of this property is said to be the element type of the collection type.
Following is an example of a class that can be enumerated:
Public Class IntegerCollection
Private integers(10) As Integer
Public Class IntegerCollectionEnumerator
Private collection As IntegerCollection
Private index As Integer = -1
Friend Sub New(ByVal c As IntegerCollection)
collection = c
End Sub
Public Function MoveNext() As Boolean
index += 1
Return index <= 10
End Function
Public ReadOnly Property Current As Integer
Get
If index < 0 OrElse index > 10 Then
Throw New System.InvalidOperationException()
End If
Return integers(index)
End Get
End Property
End Class
Public Sub New()
Dim i As Integer
For i = 0 To 10
integers(i) = I
Next i
End Sub
Public Function GetEnumerator() As IntegerCollectionEnumerator
Return New IntegerCollectionEnumerator(Me)
End Function
End Class
Before the loop begins, the enumerator expression is evaluated and cast to System.Collections.IEnumerable if it does not satisfy the requirements above. GetEnumerator is called on the resulting value and the return value of the function is stored in a temporary. Then at the beginning of each loop, MoveNext is called on the temporary. If it returns False, the loop terminates. If it returns True, the Current property is retrieved, coerced to the type of the iterator variable (regardless of whether the conversion is implicit or explicit), and assigned to the iterator variable; then the loop block executes. When the Next statement is reached, execution returns to the top of the loop. If a variable is specified after the Next keyword, it must be the same as the first variable after the For Each. For example, consider the following code:
Imports System
Module Test
Sub Main()
Dim i As Integer
Dim c As IntegerCollection = New IntegerCollection()
For Each i In c
Console.WriteLine(i)
Next i
End Sub
End Module
It is equivalent to the following code:
Imports System
Module Test
Sub Main()
Dim i As Integer
Dim c As IntegerCollection = New IntegerCollection()
Dim e As IntegerCollectionEnumerator
e = c.GetEnumerator()
While e.MoveNext()
i = e.Current
Console.WriteLine(i)
End While
End Sub
End Module
If the type E of the enumerator implements System.IDisposable, then the enumerator is disposed upon exiting the loop by calling the Dispose method. This ensures that resources held by the enumerator are released. If the method containing the For Each statement does not use unstructured error handling, then the For Each statement is wrapped in a Try statement with the Dispose method called in the Finally to ensure cleanup.
Note The System.Array type is a collection type, and since all array types derive from System.Array, any array type expression is permitted in a For Each statement. For single-dimensional arrays, the For Each statement enumerates the array elements in increasing index order, starting with index 0 and ending with index Length - 1. For multidimensional arrays, the indices of the rightmost dimension are increased first.
For example, the following code prints 1 2 3 4:
Imports System
Module Test
Sub Main()
Dim x(,) As Integer = { { 1, 2 }, { 3, 4 } }
Dim i As Integer
For Each i In x
Console.Write(i & " ")
Next i
End Sub
End Module
It is not valid to branch into a For Each statement block from outside the block.
ForEachStatement ::=
For Each LoopControlVariable In Expression StatementTerminator
[ Block ]
Next [Expression ] StatementTerminator
实现的方法好多,来一个DO+Switch看看!
Private Sub Command1_Click()
Dim j As Long
Static i As Long
Do
j = Switch(i = 0, 0, i = 1, 1, i = 2, 3)
Debug.Print j
i = i + 1
Loop While j < 3
End Sub
对暴风雨的代码做了修改,呵呵!
Private Sub Command1_Click()
Dim buff() As Long
ReDim buff(2)
buff(0) = 0
buff(1) = 1
buff(2) = 3
Dim i
For Each i In buff()
Debug.Print i
Next
Erase buff
End Sub
要不这样吧:
.....
dim SearchString as String
SearchString="0,2,3"
for xx=StartValue to EndValue
if instr(SearchString,str(xx))>0 then
.....
end if
next
......