[英]C# Left-Hand Side Of An Assignment Must Be a Variable, Property or Indexer
我真的找不到能回答這種特定情況的帖子。 再加上也許我真的很累。 無論如何,我正在為WinForms進行登錄身份驗證。 我有一個名為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對象更改為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)
{
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.