创建登录/注册表单

时间:2017-08-31 17:19:14

标签: vb.net

我在vb表单上工作,这允许一个人创建一个"帐户"。我将用户名和密码存储在两个数组中,并从中提取信息。但是当我运行该程序时,它出现了一个问题:

  

"未处理的类型' System.ArgumentNullException'   发生在Microsoft.VisualBasic.dll,附加信息:值   不能为空。"

其中Button2 / Register Button的代码是(确切地说:

 For i = 0 To (UBound(Usernames))

你能帮助我,告诉我不同​​的做法/如何处理这种情况?这是代码:

Public Class Form1

        Dim Usernames() As String
        Dim Passwords() As String
        Dim CurrName As String
        Dim i As Integer
        'Login button is pressed
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Index As Integer
            CurrName = TextBox1.Text
            For i = 0 To (UBound(Usernames))
                If IfRepetition(Usernames, CurrName, i) = True Then
                    Index = Array.IndexOf(Usernames, TextBox1.Text)
                    If TextBox2.Text = Passwords(Index) Then
                        Form3.Show()
                        Me.Hide()
                    End If
                Else
                    MsgBox("The username or password is incorrect", MsgBoxStyle.Critical)
                End If
            Next
        End Sub

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            CurrName = TextBox1.Text

            ' *** Error (apparently) happens here ***
            For i = 0 To (UBound(Usernames))
                If IfRepetition(Usernames, CurrName, i) = True Then
                    MsgBox("This username already exists!")
                Else
                    ReDim Preserve Usernames(UBound(Usernames) + 1)
                    Usernames(UBound(Usernames)) = TextBox1.Text

                    ReDim Preserve Passwords(UBound(Passwords) + 1)
                    Passwords(UBound(Passwords)) = TextBox2.Text
                End If
            Next
        End Sub
        Private Function IfRepetition(ByRef Usernames() As String, CurrName As String, i As Integer) As Boolean
            Dim j As Integer
            'Checks for repetition of a username in the usernames array
            IfRepetition = False

            For j = 0 To (UBound(Usernames))
                If Usernames(j) = CurrName Then
                    IfRepetition = True
                    Exit Function
                End If
            Next

        End Function
End Class

1 个答案:

答案 0 :(得分:0)

您获得的错误是因为如果数组为null,UBound会抛出异常。 同样使用数组也不是一个好方法,我建议你使用Dictionary来更简单,更短的代码。

Dim dic As New Dictionary(Of String, String) 'A new dictionary which handles usernames & passwords

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Login button
    If String.IsNullOrWhiteSpace(TextBox1.Text) Then 'Check the entered username, if it's empty show error message and return ( don't continue )
        MessageBox.Show("Enter the username")
        Return
    End If
    If String.IsNullOrWhiteSpace(TextBox2.Text) Then 'Check the entered password, if it's empty show error message and return ( don't continue )
        MessageBox.Show("Enter the password")
        Return
    End If

    If Not dic.ContainsKey(TextBox1.Text) Then 'Check the entered username, if it doesn't exist show error message and return ( don't continue )
        MessageBox.Show("The username is incorrect")
        Return
    End If
    If dic(TextBox1.Text) <> TextBox2.Text Then 'Validate entered username and password, if it's wrong show error message and return ( don't continue )
        MessageBox.Show("The password is incorrect")
        Return
    End If
    Form3.Show() 'If none of above error messages appear which means the entered username and password are correct, show Form3 and hide this form
    Me.Hide()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'create account button
    If String.IsNullOrWhiteSpace(TextBox1.Text) Then 'Check the entered username, if it's empty show error message and return ( don't continue )
        MessageBox.Show("Enter the username")
        Return
    End If
    If String.IsNullOrWhiteSpace(TextBox2.Text) Then 'Check the entered password, if it's empty show error message and return ( don't continue )
        MessageBox.Show("Enter the password")
        Return
    End If

    If dic.ContainsKey(TextBox1.Text) Then 'Check the entered username, if it exists show error message and return ( don't continue )
        MessageBox.Show("This username already exists")
        Return
    End If

    dic.Add(TextBox1.Text, TextBox2.Text) 'If none of above error messages which means it's okay to create this account, Add the username & password to the dictionary
End Sub