3

我想知道是否可以在 Next 语句之前获得 For Each 中 Next 值的值

如果我有这个代码:

For Each i As String In myStringArray
   'Do Something with i
Next

我可以有类似的东西,只有我正在做或检查下一个值:

For Each i As String In myStringArray
   'Do Something with i
   'Check the Next Value of i
Next
4

3 回答 3

3

您可以做相反的事情并保留最后一项,但要采取行动:

Dim last As String = Nothing

For Each i As String In myStringArray
    If last IsNot Nothing Then
        ' Treat last as current, i as next
    End If

    last = i
Next
于 2013-09-08T02:54:57.427 回答
0

不要使用for each循环,使用for循环并索引到数组中。

For index As Integer = 0 To myStringArray.Length
    ...
Next
于 2013-09-08T02:52:30.070 回答
0

No not in a for each,但如果它是一个数组,你可以使用 index 代替

For i As Integer = 0 To myStringArray.Length - 1
    Dim s As String = myStringArray(i)
    'Do Something with s
    If i - 1 < myStringArray.Length Then
        Dim nexts as String = myStringArray(i + 1)
        'Check the Next Value of s = nexts
    End If
Next
于 2013-09-08T02:54:30.993 回答