首页 > 解决方案 > 变量在循环时会覆盖自己

问题描述

我正在尝试创建一些代码,在字典中搜索单词以查找引用,但是在找到它的第一个单词后,它会在再次循环时覆盖自身。

我已经尝试将所有具有存储在其中的值的变量放入数组中,但是当我这样做时,我得到了对象引用错误的基调。

  For x = 0 To 58110
        For i = 0 To 58110
            mypos(i) = 1
            mypos(i) = InStr(password, dictionary_english(i))
            If mypos(i) > 0 Then

                word1 = dictionary_english(i)
                If Not String.IsNullOrEmpty(word1) Then found_word = True
                MsgBox(mypos(i))
                MsgBox(word1)
                If found_word = True Then
                    mylengthpassword = Len(password)
                    mylengthdictionary = Len(word1)
                    Dim secstr As String
                    Dim finalstr As String
                    Dim befstr As String
                    secstr = password.Substring(mypos(i) - 1)
                    finalstr = secstr.Substring(mylengthdictionary)
                    befstr = Mid(password, 1, mypos(i) - 1)
                    MsgBox(word1)
                    MsgBox(secstr)
                    MsgBox(word1)
                    MsgBox(password)
                    MsgBox(befstr)

                End If
            End If
        Next
    Next

我想要的是将所有已建立的单词存储到某种列表或数组中,并在其中找到它们的位置。

标签: vb.net

解决方案


我无法完全了解你在做什么检查。这是我能来的最接近的地方。英语词典也可以很容易地成为一个数组。

Private dictionary_english As New List(Of String)

Private Function CheckStrength(password As String) As String
    For Each word In dictionary_english
        If word.Contains(password) Then
            Return "Password contained in Dictionary"
            'The function will stop checking as soon as it finds a match
        End If
    Next
    Return "Password is not found in dictionary"
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim message = CheckStrength(txtPassword.Text)
    MessageBox.Show(message)
End Sub

推荐阅读