C# Left-Hand Side Of An Assignment Must Be a Variable, Property or Indexer

僤鯓⒐⒋嵵緔 提交于 2020-01-28 10:25:30

问题


I couldn't really find posts that answered this specific scenario. Plus maybe I am just really tired. Anyway, I am working on a login authentication for WinForms. I have a class named DBFunctions.cs which hold database connection info etc. I am stuck with this "The left-hand side of an assignment must be a variable, property or indexer" error in C#. Please find my current code below. Thanks in advance.

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();
    }
    }

    }
    }
    }

回答1:


Nice easy one, you're confusing = (the assignment operator) with == (the comparison operator).

You would be meaning to enter

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

(rather than = true)

But really, comparing a boolean value with a constant boolean value is a redundant operation.

You should just use:

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



回答2:


I was trying to convert an object to bool. I declared status of type bool and returned a bool so, I needed to change public object to public bool. Code follows:

Original:

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;
    }

Corrected

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)
        {


来源:https://stackoverflow.com/questions/41627046/c-sharp-left-hand-side-of-an-assignment-must-be-a-variable-property-or-indexer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!