Answer:
VB:
Public Function VerifyUsername(ByVal username As String) As Boolean
For positionInUsernameArray As Integer = 0 To arrUsername.Length - 1
If positionInUsernameArray = arrUsername.Length Then Exit For
If username = arrUsername(positionInUsernameArray) Then
Return True
Else
Continue For
End If
Next
Return False
End Function
Public Function VerifyPassword(ByVal password As String) As Boolean
For positionInUsernameArray As Integer = 0 To arrPassword.Length - 1
If positionInUsernameArray = arrPassword.Length Then Exit For
If password = arrPassword(positionInUsernameArray) Then
Return True
Else
Continue For
End If
Next
Return False
End Function
C#:
sealed class Main
{
internal bool blnLoggedIn;
string[] arrUsername = new string[] { "Admin", "Clerk", "Manager" };
string[] arrPassword = new string[] { "password", "password2", "password3" };
public void Login(string username, string passowrd)
{
blnLoggedIn = false;
if (VerifyUsername(username) && VerifyPassword(passowrd))
{
// Find index for username
int userIndex = 0;
for (int loopIndex = 0; loopIndex <= arrUsername.Length - 1; loopIndex++)
{
if (arrUsername[loopIndex] == username)
{
userIndex = Convert.ToInt32(loopIndex);
break;
}
else continue;
}
// Check for password match
if (arrPassword[userIndex] == passowrd)
blnLoggedIn = true;
else
MessageBox.Show("Incorrect password");
}
}
public bool VerifyUsername(string username)
{
for (int positionInUsernameArray = 0; positionInUsernameArray <= arrUsername.Length - 1; positionInUsernameArray++)
{
if (positionInUsernameArray == arrUsername.Length)
break;
if (username == arrUsername[positionInUsernameArray])
return true;
else continue;
}
return false;
}
public bool VerifyPassword(string password)
{
for (int positionInUsernameArray = 0; positionInUsernameArray <= arrPassword.Length - 1; positionInUsernameArray++)
{
if (positionInUsernameArray == arrPassword.Length)
break;
if (password == arrPassword[positionInUsernameArray])
return true;
else continue;
}
return false;
}
}
Step-by-step explanation:
First I created a for loop with a "positionInUsernameArray" and singed it a value of 0. Next I stated while positionInUsernameArraywas less than or equal to arrUsername.Length - 1, positionInUsernameArraywould increment.
Once the base for loop was in place I created two if statements. The first if statement determines if positionInUsernameArray is equal to arrUsername.Length, if so, the for loop breaks and the method returns false. The second if statement determines if the parameter is equal to the position in the arrUsername array, if so, the method would return true, otherwise the for loop will restart.
For the "VerifyPassword" method, I used the same method as "VerifyUsername" just I changed the variable names.
Hope this helped :) If it didn't please let me know what happened so I can fix the code.
Have a good day!