lundi 13 mai 2019

How to run though loop with foreach and IF-statement to check input value is found in dictionary

I have created a simple login page, without using database, i typed the succesfuld logins in a dictionary and want to run through the dictionary with a foreach loop, and check if input values are found in dictionary. if so then succesfuld login, else and error. i just can't figure out how to run through the dictionary and check if input value match dictionary values (Name and password)

public class EmployeeCollection
{
    private string _name;
    private int _password;

    private EmployeeCollection()
    {

        Dictionary<int, Employee> Employees = new Dictionary<int, Employee>();
        Employees.Add(1, new Employee("qwe", 12345));
        Employees.Add(2, new Employee("asd", 12345));
        Employees.Add(3, new Employee("zxc", 12345));
    }
    public Dictionary<int, Employee> Employees = new Dictionary<int, Employee>();

    public bool RequestLogin(string Username, int Password)
    {
        foreach (var item in Employees) // Dictionary
        {
            if (Employees.ContainsKey(1))
            {
                return true;
            }
            else if (Employees.ContainsKey(2))
            {
                return true;
            }
            else if (Employees.ContainsKey(3))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;

public class LoginPageViewModel : ViewModelBase
    {
        private EmployeeCollection _employee;
        private EmployeeCollection _employeeCollection;
        private string _username;
        private int _password;

        public LoginPageViewModel()
        {

            LoginCommand = new RelayCommand(toLoginCommand);
        }

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        public int Password
        {
            get { return _password; }
            set { _password = value; }
        }
        public RelayCommand LoginCommand { get; set; }

        public void toLoginCommand()
        {
            _employee.RequestLogin(Username, Password);
        }

Aucun commentaire:

Enregistrer un commentaire