如何制作面向对象的登录程序?



下面的代码有什么问题?我的代码给了我一些错误。我想在我们的项目中使用面向对象的程序,但它不能正常工作。有人可以帮助我解决我的问题吗

class Login
{
public string Username { get; set; }
public string Userpassword { get; set; }
public Login()
{
this.Username = user; //does not exist in the current context
this.Userpassword = pass; //does not exist in the current context
}
private void ClearTexts(string user, string pass)
{
user = String.Empty;
pass = String.Empty;
}
public void Login(string user, string pass)
{
int count = 0;
Connection connection = new Connection();
string sql = "SELECT * FROM tbl_Login";
MySqlConnection conn = new MySqlConnection(connection.ConnectionString);
MySqlCommand cmd = new MySqlCommand(sql, conn);
conn.Open();
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Login Successfully!");
frmLogin.Hide(); //required for the non-static field,method
frmMain.showMe(this); //best overloaded method match...
}
else
{
txtPassword.Focus(); /does not exist in the current context
MessageBox.Show("Username or Password Is Incorrect");
txtUserName.Text = ""; //does not exist in the current context
txtPassword.Text = ""; //does not exist in the current context
}
connection.Close();
}
}

你不需要没有参数的Login()构造函数。而是使用如下参数将字段设置向下移动到构造函数:

public void Login(string user, string pass)
{
this.Username = user;
this.Userpassword = pass;
// more code here
}
public Login(){
this.Username = user; //does not exist in the current context
this.Userpassword = pass; //does not exist in the current context
}

userpass不是函数的参数,因此它们不存在。你已经有一个接受用户并传递的构造函数,所以这个构造函数应该将它们设置为 null 或根本不存在,并强制使用 user/pass 构造函数

最新更新