In the class "CaseMethods" and method "ChangeUsername" it is supposed to change the user's username. But, in an else statement (pretty far down) I have the statement:
uap.users[ipos] = newUserName;
This is what actually changes the username but when I try to "login" with the new username, it doesn't work. Instead, it still uses the original username.
This is the relevant code:
The class UsernamesAndPasswords include the usernames and passwords for the "login"
The method Login is the login
public class UsernamesAndPasswords
{
public string[] users = { "admin", "WinstonMillward", "EdwenaSweet", "LiviaDennell", "EugeneAddison", "MarshaAdams", "", "", "", "", "", "", "", "", "" };
public string[] pswrds = { "password", "WM13051977", "ES17061985", "LD24091994", "EA12101996", "MA03031999", "", "", "", "", "", "", "", "", "" };
}
public class CaseMethods
{
public void ChangeUsername(int ipos)
{
var uap = new UsernamesAndPasswords();
Console.WriteLine("Enter your current username: ");
if (Console.ReadLine() == uap.users[ipos])
{
EnterUsername();
void EnterUsername()
{
Console.WriteLine("Enter your new username: ");
string newUserName = Console.ReadLine();
ConfirmUsername(newUserName);
}
void ConfirmUsername(string newUserName)
{
Console.WriteLine("Confirm your new username: ");
if (newUserName != Console.ReadLine())
{
UsernamesDontMatch();
void UsernamesDontMatch()
{
Console.WriteLine("Those usernames do not match. To change your new password type \"new\". To try to confirm your username again type \"try again\"");
switch (Console.ReadLine())
{
case "new":
EnterUsername();
break;
case "try again":
ConfirmUsername(newUserName);
break;
default:
Console.WriteLine("That is not one of the options.");
UsernamesDontMatch();
break;
}
}
}
else
{
uap.users[ipos] = newUserName;
Console.WriteLine($"Username has been changed to {newUserName}.\nPlease login using your new username.");
var cl = new CompanyLogin();
cl.Login();
}
}
}
else
{
Console.WriteLine("That is not your current username.");
ChangeUsername(ipos);
}
}
}
public void Login()
{
var uap = new UsernamesAndPasswords();
string indexPosIn = Console.ReadLine();
int ipos = Convert.ToInt32(indexPosIn);
string iuser = Console.ReadLine();
string ipswrd = Console.ReadLine();
switch ((iuser, ipswrd))
{
case (var plA, var plB) when iuser == uap.users[0] && ipswrd == uap.pswrds[0]:
Admin();
break;
case (var plA, var plB) when iuser == uap.users[ipos] && ipswrd == uap.pswrds[ipos] && iuser != "admin":
User(iuser, ipos);
break;
default:
Console.WriteLine("Incorrect. Try Again.");
Login();
break;
}
}
I have tried just looking through the code to see if anything would prevent that part from being able to change the value in the UsernamesAndPasswords class. I didn't find anything that could've. I would've searched online but I don't really know what I could've searched to solve this problem.
CodePudding user response:
You are creating a new instance of the UsernamesAndPasswords
class in both the ChangeUsername
and Login
methods. These instances are unrelated so the change you make in ChangeUsername
is lost when the variable uap
goes out of scope at the end of the method. You could have something like this:
public class CaseMethods
{
private UsernamesAndPasswords aup = new UsernamesAndPasswords();
public void ChangeUsername(int ipos)
{
...
this.uap[...]; // use the shared member
...
}
public void Login()
{
...
this.uap[...]; // use the shared member
...
}
}
CodePudding user response:
As HasaniH pointed out, you're creating two separate instances of UAP (Username and password).
In order to prevent that from occurring I would strongly advise refactoring the UsernameAndPasswords
class to be a class with two fields named username
and password
with appropriate setters and getters and wherever the starting function is, create your array of user accounts with the data you have in those arrays already so you are essentially creating an array of UsernameAndPasswords
objects (I'd probably rename the class to accounts
to be a more general name as well).
This way you have one centrally located array of UsernameAndPasswords
objects that can then be passed to the respective helper functions you have.
Regarding the helper functions in particular, I would recommend defining each function individually and not within each other, it will make reading and debugging your code easier. This is because it allows the functions to be quickly reusable and not scoped locked to the function it's defined in.