For Each,Next之前的下一个值

时间:2013-09-08 02:49:11

标签: vb.net foreach

我想知道我是否可以在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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

不要使用for each循环,使用for循环并将索引编入数组。

For index As Integer = 0 To myStringArray.Length
    ...
Next

答案 2 :(得分:0)

不是每个都没有,但如果它是一个数组

你可以使用索引
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