简体   繁体   中英

How to add more than one item into dictionary from user input

I am new to C# and I am trying to add more than one element in the dictionary using user input. I tried different combinations, but still I can only see 1 entry when I test the program. You can find below my registration method which is a part of a 3 option menu. I am using the dictionary as a public class and created a property so I can use it in different methods. I don't know if my logic is not correct, but I would appreciate some feedback.

private static void Register()
{
    userData = new Dictionary<string, string>();

    //input for user to add username and password 
    string userName = "";
    string password = "";

    SystemMessage("Enter a Username :");
    userName = Console.ReadLine();

    SystemMessage("Enter a Password :");
    password = Console.ReadLine();

    //add username and password in to dictionary
    userData.Add(userName, password);

    //check that username is not the same aas the password for better security
    if (userName.Equals(password))
    {
        ErrorMesssage("Username and Password cannot be the same");
    }

    if (userData.Equals(userName))
    {
        ErrorMesssage($"Username: {userName} already exist!");
    }
    else
    {
        //get count of key/value items
        Console.WriteLine(userData.Count);
    }
}

A quick approach would be to make you dictionary a static like

private static Dictionary<string, string> userData = new Dictionary<string, string>();

In you current implementation every time that you call the Register method you create a new Dictionary that is of course empty.

General feedback

  1. You process your data in wrong order

     userData.Add(userName, password); //check that username is not the same aas the password for better security if (userName.Equals(password)) { ErrorMesssage("Username and Password cannot be the same"); } if (userData.Equals(userName)) { ErrorMesssage($"Username: {userName} already exist;"); }

You first make the validation checks (username same as password and key exists) and then add to distionary.

  1. In order to check if you have an entry in your dictionary you do not use Equal method but userData.ContainsKey(userName)

So the code should look like

    //check that username is not the same aas the password for better security
    if (userName.Equals(password, StringComparison.OrdinalIgnoreCase))
    {
        ErrorMesssage("Username and Password cannot be the same");
    }
    if (userData.ContainsKey(userName))
    {
        ErrorMesssage($"Username: {userName} already exist!");
    }
    else
    {
        //get count of key/value items
        Console.WriteLine(userData.Count);
    }
    userData.Add(userName, password);
  1. Since it is a password, even for a uni project it might be a good idea to use some hashing at least so it is not plane test before you persist it somewhere

You are redefining the dictionary every time you call Register()

Redefine the userName Dictionary outside of the method, either in main() or outside of any method globally. This will sort out your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM