C# Constructor has 2 arguments, but claims it does

2019-07-08 02:31发布

So here is my problem. I have a class called Login that will be used for logging in and creating new log in accounts.

I've created a Login constructor that takes no arguments

public Login()
{
    _gloID = 0;
    _Username = null;
    _Password = null;
    _Note = null;
    _Active = false;
    _Status = null;
    _gvoID = 0;
    _DateModified = new DateTime(1901, 1, 1);
    _ModifiedBy = 0;
}

I've also created a Login constructor that takes two arguments. This constructor takes the username and password and then gathers the rest of the information from the database.

public Login(string username, string password)
{
    // Declarations
    uint gloid = 0, gvoid = 0, modifiedby = 0;
    string note = null, status = null;
    bool active = false;
    DateTime datemodified = new DateTime(1901, 1, 1);
    // Command
    string query = string.Format("SELECT glo_id, glo_note, glo_active, glo_status, gvo_id, date_modified, modified_by FROM gfrc_login" +
                                    " WHERE glo_username = '{0}' AND glo_password = '{1}'", username, password);

    try
    {
        using (conn)
        {
            conn.Open();
            cmd = new OleDbCommand(query, conn);
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                gloid = Convert.ToUInt32(rdr.GetString(0));
                note = rdr.GetString(1).ToString();
                active = Convert.ToBoolean(rdr.GetString(2));
                status = rdr.GetString(3).ToString();
                gvoid = Convert.ToUInt32(rdr.GetString(4));
                datemodified = Convert.ToDateTime(rdr.GetString(5));
                modifiedby = Convert.ToUInt32(rdr.GetString(6));
            }
        }
    }
    finally
    {
        if (rdr != null)
            rdr.Close();
    }
    if (conn != null)
    {
        conn.Close();
    }

    _gloID = gloid;
    _Username = username;
    _Password = password;
    _Note = note;
    _Active = active;
    _Status = status;
    _gvoID = gvoid;
    _DateModified = datemodified;
    _ModifiedBy = modifiedby;
}

Note that all the database connection variables have been declared at the beginning of the class.

Now when I try to run the following I get an error saying: 'Login' does not contain a constructor that takes 2 arguments.

protected void Login_Authenticate(object sender, EventArgs e)
{
    string username = txtUsername.Text;
    string password = CalculateMD5(txtPassword.Text);
    Login login = new Login(username, password);
}

EDIT: FYI, I have measures preventing SQL injections in the rest of my code.

2条回答
女痞
2楼-- · 2019-07-08 02:38

The class you are calling from is called Login as well, I can tell that from the event handler that's been generated.

You must use the full namespace of the type or rename the class to something else.

For example:

protected void Login_Authenticate(object sender, EventArgs e)
{
    string username = txtUsername.Text;
    string password = CalculateMD5(txtPassword.Text);
    My.Namespace.Login login = new My.Namespace.Login(username, password);
}
查看更多
We Are One
3楼-- · 2019-07-08 02:47

You are probably referring to two different Login classes. Try specifying the full name (with the namespace) and see what happens.

查看更多
登录 后发表回答