简体   繁体   English

'Final_Project.Member'不包含带有2个参数的构造函数

[英]'Final_Project.Member' does not contain a constructor that takes 2 arguments

      while (dbReader.Read())
            {
                aMember = new Member(dbReader["FirstName"].ToString(),                 dbReader["LastName"].ToString());
                this.lstbxNames.Items.Add(aMember);

            }
            dbReader.Close();
            dbConn.Close();
        }



   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;

 namespace Final_Project
{
     class Member
     {
    private string firstname;
    private string lastname;
    private string currentbalance;

    public Member()
    {

    }

    public Member(string lname)
    {
        string LastName = lname;
    }

    public string FirstName
    {
        get { return firstname; }
        set { firstname = value; }
    }

    public string LastName
    {
        get { return lastname; }
        set { lastname = value; }
    }

    public string CurrentBalance
    {
        get { return currentbalance; }
        set { currentbalance = value; }
    }
}

} }

I am not sure how to fix this error. 我不确定如何解决此错误。 I have created a class for 'Member' but I am still getting the error that says it does not contain a constructor that takes 2 arguments. 我已经为'Member'创建了一个类,但是仍然出现错误,提示它不包含带有2个参数的构造函数。 I have added the first part of the code that has the error and the class I created that should have made it work. 我在代码的第一部分添加了错误,并添加了我应该创建的类。

aMember = new Member(dbReader["FirstName"].ToString(), dbReader["LastName"].ToString());

so lets pretend those values end up looking like this 因此,让我们假装这些值最终看起来像这样

aMember = new Member("Bill","Gates");

so rather than a comma, maybe you need to join those fields together?? 因此,您可能需要将这些字段结合在一起,而不是逗号?? or change your creation so you send it first and surname, and set both fields as you have option for both in your class. 或更改您的创建内容,以便您先发送它和姓氏,然后在班级中将这两个字段都设置为具有选项。

Add a new constructor to let you pass in first name and last name, then set your properties in it. 添加一个新的构造函数,让您传递名字和姓氏,然后在其中设置属性。 Currently you only have a constructor that accepts one argument that then sets the LastName property. 当前,您只有一个接受一个参数然后再设置LastName属性的构造函数。

public Member(string lname)
{
    LastName = lname;
}

//New overloaded constructor for lastname and firstname.
public Member(string lname, string fname)
{
    LastName = lname;
    FirstName = fname;
}

Modifying a code line you have that is attempting to intialize a new member: 修改您尝试初始化一个新成员的代码行:

aMember = new Member(dbReader["LastName"].ToString(), dbReader["FirstName"].ToString());

If you would rather make it firstName, lastName for initialization, you could flip the constructor arguments around. 如果希望将它设为firstName,lastName进行初始化,则可以翻转构造函数参数。

Maybe you want your Final_Project.Member constructor as 也许您希望将Final_Project.Member构造函数用作

 public Member(string firstName, string lastName)
{
    this.firstname = firstName;
    this.lastname = lastName;
}

this will work fine. 这会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM