Can someone tell me if I did this the right way so far?? I know im missing part of the IF..Catch statement still..Im working on that.
Steps that I had to follow:
1. Make the LoginException class a custom exception by deriving from the Exception class.
2. In the Login procedure of the Main module, replace the two lines that read MessageBox.Show(“Incorrect password.”) with the following code: Throw New LoginException.
3. Add a Try...Catch statement for the following line in the btnLogin_Click event handler procedure: Main.Login(txtUsername.Text, txtPassword.Text) Add the following line to the Catch block: MessageBox.Show(“Incorrect password.”)
This is my LoginException code:
Code:Public Class LoginException Inherits Exception End Class
This is my Main.vb code
Code:Module Main Friend blnLoggedIn As Boolean Dim arrUsernames() As String = {"Admin, Clerk, Manager"} Dim arrPasswords() As String = {"P@ssword, pa$$word, and passw0rd"} Sub Login(username As String, password As String) blnLoggedIn = False If VerifyUsername(username) And VerifyPassword(password) Then 'Find index for username Dim userIndex As Integer For loopIndex = 0 To arrUsernames.Length - 1 If arrUsernames(loopIndex) = username Then userIndex = loopIndex Exit For End If Next 'Check for password match If arrPasswords(userIndex) = password Then blnLoggedIn = True Else Throw New LoginException End If End If End Sub Function VerifyUsername(username As String) As Boolean Return True 'If the username is found, Return True, otherwise Return False End Function
And this is my LoginForm code:
Code:Public Class LoginForm Private Sub Label1_Click(sender As Object, e As EventArgs) Handles lblUsername.Click, btnLogin.Click Login(txtUsername.Text, txtPassword.Text) Try Catch ex As LoginException MessageBox.Show(“Incorrect password.”) End Try If Main.blnLoggedIn Then MessageBox.Show(“Thank you for logging in, “ & txtUsername.Text, “Logged In.”) Me.Close() End If End Sub Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click Application.Exit() End Sub End Class




Reply With Quote
