我认为问题出在list.Contains部分,因为其他一切似乎都有效,但是idk
我的最终目标是拥有一种本地注册/登录系统(无需使用任何数据库或sql的东西)
以下是代码:
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));
}
答案 0 :(得分:1)
当调用List.Contains时,它会检查列表中的确切对象,但新客户是一个不同的对象,即使它具有相同的值。尝试更改您的客户Equals方法以比较用户名和密码,而不是再次调用Customer.Equals。
答案 1 :(得分:1)
public void Login()
{
if (_list.Contains(new Customer {Name = Username2, Pass = Password2}))
CanLogin = true;
}
这可能无法正常工作,List.Contains
检查对象是否已在列表中。但是在这里,你正在用一些数据创建一个全新的对象,这个新对象肯定会不在列表中。我更适合找到具有这些属性的客户,或者更好,看看是否存在这些属性。您可以使用List.Find()
方法执行此操作,该方法将使用lambda表达式将Customer
映射到bool
。你可以这样做:
if (_list.Find(c => c.Name == Username2 && c.Pass == Password2) != null)
CanLogin = true;
Alternativley实现Equals(Customer otherCustomer)
函数并使用相等函数检查列表中是否存在这样的用户。
答案 2 :(得分:0)
我可以在您的代码中看到更多问题。你有一个不需要任何参数的构造函数。这可能导致其他开发人员创建没有用户名和密码的Customer对象,然后调用LogIn()
或Register()
等方法。因此,最好删除该构造函数或将用户名,密码作为Login()
和Register()
方法中的参数。您正在使用注册方法将客户分配到_list
。但是,当您致电_list
时,LogIn()
很可能没有任何客户。所以你可能需要再次考虑你的设计。
public void Login(string username, string password)
{
CanLogin = (_list.Any(c => c.Name.Equals(username, StringComparison.CurrentCultureIgnoreCase) && c.Pass.Equals(password,StringComparison.CurrentCulture));
}
答案 3 :(得分:0)
List.Contains没有问题,因为您已经覆盖了Equals运算符。我宁愿使用哈希码使名称不可变为安全。我完全不理解你的代码中的意图,但构建CustomerList似乎是可疑的,它不应该保持行为之间的状态吗?我重写了代码,所以它对我有意义 - 行为已经改变(输出是#34;通过&#34;):
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");
}
}