Reputation: 3
sorry for having to ask a stupid looking question, but I tried all the advice for the last atleast 5 hours. and it hasnt made it any clearer. Im a beginner programmer and I cant see why it wouldnt work, theres no compile errors so I need your help here.
I think the problem is in the list.Contains part because everything else seems to work, but idk
my end goal was to have a sort of local register/login system (without having to use any database or sql stuff)
Here is the code:
class CustomerList
{
private string _x = "x";
private string _y = "y";
private string _x2 = "x2";
private string _y2 = "y2";
private List<Customer> _list;
//
public string Username
{
get { return _x; }
set { _x = value; }
}
// these are for the register
public string Password
{
get { return _y; }
set { _y = value; }
}
//
public CustomerList()
{
_list = new List<Customer>();
}
//
public string Username2
{
get { return _x2; }
set { _x2 = value; }
}
//these are for the login
public string Password2 {
get { return _y2; }
set { _y2 = value; } }
//
public void Register()
{
_list.Add(new Customer { Name = Username, Pass = Password });
}
public void Login()
{
if (_list.Contains(new Customer {Name = Username2, Pass = Password2}))
CanLogin = true;
}
public bool CanLogin { get; set; } = false;
}
namespace App10.Model
{
public class Customer : IEquateable<Customer>
{
private string _password = "";
private string _username = "";
public Customer()
{
}
public Customer(string username, string password)
{
this.Pass = password;
this.Name = username;
}
public string Name
{
get { return _username; }
set { _username = value; }
}
public string Pass
{
get { return _password; }
set { _password = value; }
}
public bool Equals(Customer other)
{
if (other == null)
return false;
if (this._username == other._username)
return true;
else
return false;
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
Customer customerObj = obj as Customer;
if (customerObj == null)
return false;
else
return Equals(customerObj);
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public static bool operator ==(Customer person1, Customer person2)
{
if (((object)person1) == null || ((object)person2) == null)
return Object.Equals(person1, person2);
return person1.Equals(person2);
}
public static bool operator !=(Customer person1, Customer person2)
{
if (((object)person1) == null || ((object)person2) == null)
return !Object.Equals(person1, person2);
return !(person1.Equals(person2));
}
}
public interface IEquateable<T>
{
}
}
private void register_Click_1(object sender, RoutedEventArgs e)
{
CustomerList ss = new CustomerList();
ss.Username = textBox.Text;
ss.Password = textBox1.Text;
ss.Register();
Frame.Navigate(typeof(BlankPage2));
}
private void login_Click(object sender, RoutedEventArgs e)
{
CustomerList ss2 = new CustomerList();
ss2.Login();
if (ss2.CanLogin)
{
Frame.Navigate(typeof(MainPage));
}
Upvotes: 0
Views: 110
Reputation: 2143
I can see couple more issues in your code. You have a constructor that doesn't need any parameters. This can cause another developer to create a Customer object without username and password and then call methods like LogIn()
or Register()
. So it is better remove that constructor or have username, password as parameters in Login()
and Register()
methods. You are assigning customers to _list
in register method. But it is more than likely that _list
will not have any customers when you call LogIn()
. So you may need to think about your design again.
public void Login(string username, string password)
{
CanLogin = (_list.Any(c => c.Name.Equals(username, StringComparison.CurrentCultureIgnoreCase) && c.Pass.Equals(password,StringComparison.CurrentCulture));
}
Upvotes: 0
Reputation: 6103
List.Contains works without problem as You have overriden the Equals operator. I would rather make the Name immutable to be safe with the hashcode. I did not understand the intentions in Your code completely, but building CustomerList seems suspicious, should not it preserve state between actions? I rewrote the code so it makes sense for me - behaviour has changed (the output is "Passed"):
class CustomerList
{
private List<Customer> _list = new List<Customer>();
public void Register(string user, string password)
{
_list.Add(new Customer(user, password));
}
public bool CanLogin(string user, string password)
{
return _list.Contains(new Customer(user, password));
}
}
public class Customer
{
public string Name { get; protected set; }
public string Pass { get; set; }
public Customer(string username, string password = "")
{
Name = username;
Pass = password;
}
public bool Equals(Customer other)
{
if (other == null)
return false;
return this.Name == other.Name;
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
Customer customerObj = obj as Customer;
if (customerObj == null)
return false;
else
return Equals(customerObj);
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public static bool operator ==(Customer person1, Customer person2)
{
if (((object)person1) == null || ((object)person2) == null)
return Object.Equals(person1, person2);
return person1.Equals(person2);
}
public static bool operator !=(Customer person1, Customer person2)
{
return !(person1 == person2);
}
}
private void button30_Click(object sender, EventArgs e)
{
CustomerList ss = new CustomerList();
ss.Register("Tony", "xx");
if (ss.CanLogin("Tony", "xx"))
{
Console.WriteLine("Passed");
}
}
Upvotes: 0
Reputation: 5353
public void Login()
{
if (_list.Contains(new Customer {Name = Username2, Pass = Password2}))
CanLogin = true;
}
This can't possibly work, List.Contains
checks if an object is already in the list. But here, you are creating a brand new object with some data, and this new object will definetly not be in the list. I'd be more appropiate find a customer which has these attributes, or better, see if there exists one. You can do so by using the List.Find()
method, which will take a lambda expression which maps a Customer
to a bool
. You could do e.g.:
if (_list.Find(c => c.Name == Username2 && c.Pass == Password2) != null)
CanLogin = true;
Alternativley implement an Equals(Customer otherCustomer)
function and use the equality function to check if there exists such a user in the list.
Upvotes: 1
Reputation: 233
When List.Contains is called, it's checking for that exact object being in the list, but a new Customer is a different object, even if it holds the same values. Try changing your customer Equals method to compare the username and password instead of calling Customer.Equals again.
Upvotes: 1