创建登录/寄存器表格



我正在使用VB表单,该表格允许一个人创建一个"帐户"。我将用户名和密码存储在两个数组中,然后从它们中提取信息。但是当我运行程序时,它会遇到一个问题:

"类型'system.argumentNullexception'的未手动例外' 发生在Microsoft.visualbasic.dll中,附加信息:值 不能无效。"

the Button 2/寄存器按钮的代码在哪里(确切地说:

 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

您遇到的错误是因为ubound会引发异常,如果数组为null。同样使用阵列不是一个好方法,我建议您使用字典以更简单,较短的代码。

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

最新更新