C#左侧的分配必须是变量,属性或索引器

我无法找到能够解决这一特定情况的post。 也许我真的很累。 无论如何,我正在为WinForms进行登录validation。 我有一个名为DBFunctions.cs的类,它保存数据库连接信息等。我坚持使用C#中的“赋值的左侧必须是变量,属性或索引器”错误。 请在下面找到我当前的代码。 提前致谢。

namespace emsdashboard { public partial class Login : Form { public Login() { InitializeComponent(); } //Contains the SQL string and other information to process //user login. public object VerifyUser(string userId, string password) { DBFunctions dbInfo = new DBFunctions(); bool status = false; string verifyUserQry = "SELECT * FROM Employee WHERE UserName = '" + userId + "' AND Password = '" + password + "'"; DataTable dt = default(DataTable); dt = dbInfo.OpenDTConnection(verifyUserQry); if (dt.Rows.Count == 1) { status = true; } return status; } //When the login button is clicked. Check to see if the user //entered a username and/or password. Also verify the username //and the password are correct, else display an error message. private void btnLogin_Click(object sender, EventArgs e) { if(tbxUsername.Text=="" || tbxPassword.Text=="") { MessageBox.Show("Username and Password cannot be blank", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { if (VerifyUser(tbxUsername.Text, tbxPassword.Text) = true) { this.Hide(); } } } } } 

很容易,你很困惑= (赋值运算符) == (比较运算符)。

你会意味着进入

 if (VerifyUser(tbxUsername.Text, tbxPassword.Text) == true) 

(而不是= true

但实际上,将布尔值与常量布尔值进行比较是一项冗余操作。

你应该使用:

 if (VerifyUser(tbxUsername.Text, tbxPassword.Text)) 

我试图将一个对象转换为bool。 我宣布bool类型的状态并返回bool所以,我需要将公共对象更改为public bool。 代码如下:

原版的:

 public object VerifyUser(string userId, string password) { DBFunctions dbInfo = new DBFunctions(); bool status = false; string verifyUserQry = "SELECT * FROM Employee WHERE UserName = '" + userId + "' AND Password = '" + password + "'"; DataTable dt = default(DataTable); dt = dbInfo.OpenDTConnection(verifyUserQry); if (dt.Rows.Count == 1) { status = true; } return status; } 

更正

 public bool VerifyUser(string userId, string password) { DBFunctions dbInfo = new DBFunctions(); bool status = false; string verifyUserQry = "SELECT * FROM Employee WHERE UserName = '" + userId + "' AND Password = '" + password + "'"; DataTable dt = default(DataTable); dt = dbInfo.OpenDTConnection(verifyUserQry); if (dt.Rows.Count == 1) {