2017-08-31 25 views
0

私はvbフォームで作業しています。これは、人が「アカウント」を作成できるようにします。ユーザー名とパスワードを2つの配列に格納し、そこから情報を抽出します。しかし、私はプログラムを実行すると、それは問題を思い付く:ログイン/登録フォームを作成する

「タイプの未処理の例外 『をSystem.ArgumentNullException』 はがmicrosoft.visualbasic.dll、発生しました。追加情報:値 はnullにすることはできません。」ボタン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 
+1

「さまざまな問題があります」「インターネット上で見つけた内容を貼り付ける代わりに、 – litelite

+0

より具体的にする必要があります。あなたは多くの問題を言いますが、説明できません。 – Mederic

+0

「あらゆる種類の問題があります」では、問題については何も教えてくれません。おそらくあなたは*少し*より具体的なことができますか? – David

答えて

0

配列がnullの場合、UBound関数が例外をスローするので、あなたが取得しているエラーがある も使用してアレイには、私はあなたが簡単に、短いコードのための辞書を使用することをお勧め、良い方法ではありません。

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 
+0

ありがとう、それは完璧に動作します。あなたの助けに感謝! – aBegginer

関連する問題