C# Constructor has 2 arguments, but claims it does not have a constructor that takes two arguments

感情迁移 提交于 2019-12-24 01:28:03

问题


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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/16980741/c-sharp-constructor-has-2-arguments-but-claims-it-does-not-have-a-constructor-t

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