检查循环中的代码不能正常工作

Check code in loop doesn't working properly

我编写了一个能够在网页中下载值的代码,并且运行良好。由于取值是实时取的,也就是真正有变化的时候才下载数据,所以如果某个值发生变化我就需要。 然后我创建了一个代码,允许我从数组的所有元素中获取更新值和旧值数组的检查。 代码实际上检测到给定结果何时发生变化,但由于某些奇怪的原因,该消息似乎无限大,就好像它被卡在一个没有参数的 foor 中,这很奇怪。

For Each abc As Country_Data In lista
                For Each xyz As Country_Data In vecchia_lista
                    If abc.casa = xyz.casa And abc.ospite = xyz.ospite Then
                        If abc.Result <> xyz.Result Then
                            MsgBox(abc.casa & " - " & abc.ospite & " -- " & abc.Result)
                            Exit For
                        End If
                    End If
                Next
            Next

变量定义

Dim lista As New List(Of Country_Data)
    Dim vecchia_lista As New List(Of Country_Data)
    Private Structure Country_Data
        Dim casa As String
        Dim ospite As String
        Dim Result As String
    End Structure

如何解决这个问题?

根据您的评论,我了解到您只想看到一个消息框,当它发现第一个结果已更改的情况时(即使之后可能还有其他情况)。

您使用的 Exit For 仅从内部 For 循环退出,因此外部 For 循环继续循环,这可能就是您所观察到的。

你可以这样做:

Dim keepLooping As Boolean = True

For Each abc As Country_Data In lista
    For Each xyz As Country_Data In vecchia_lista
        If abc.casa = xyz.casa And abc.ospite = xyz.ospite Then
            If abc.Result <> xyz.Result Then
                MsgBox(abc.casa & " - " & abc.ospite & " -- " & abc.Result)
                keepLooping = False
                Exit For
            End If
        End If
    Next

    If (Not keepLooping) Then Exit For
Next

这样当你想停止循环时,它会设置布尔标志,外循环会观察到它,导致它也退出。