C#错误:不包含带0参数的构造函数

时间:2014-10-18 19:47:01

标签: c# list class listbox

我对C#很陌生,我不明白我做错了什么,这是我的代码:

public class Client
        {

            public int Code { get; set; }
            public string Name { get; set; }
            public int Phone { get; set; }
            public string Email { get; set; }

            public Client (int code, string name, int phone, string email)
            {
                Code = code;
                Name = name;
                Phone = phone;
                Email = email;

            }

            public override string ToString()
            {
            return string.Format("Code {0} | Name: {1} | Phone: {2} | Email{3}", Code, Name, Phone, Email);
            }

        }
        public frm_cadastro()
        {
            InitializeComponent();
        }

        public List<Client> clieList = new List<Client>();

        private void btn_add_Click(object sender, EventArgs e)
        {

            clieList.Add(new Client() //This is where the error is
            {
                Code = Convert.ToInt32(txt_cod.Text),
                Name = txt_name.Text,
                Phone = Convert.ToInt32(txt_phone.Text),
                Email = txt_email.Text,
            });

此代码源自上一个问题,我问我如何将列表数据加载到文本框中。

4 个答案:

答案 0 :(得分:1)

clieList.Add(new Client() //This is where the error is
{
    Code = Convert.ToInt32(txt_cod.Text),
    Name = txt_name.Text,
    Phone = Convert.ToInt32(txt_phone.Text),
    Email = txt_email.Text,
});

这不是您正在寻找的构造函数。它是无参数构造函数调用+属性初始化语法。它没有用,因为你的课没有定义无参数构造函数。

您正在寻找的是:

clieList.Add(new Client(Convert.ToInt32(txt_cod.Text),
                       txt_name.Text,
                       Convert.ToInt32(txt_phone.Text),
                       txt_email.Text));

答案 1 :(得分:1)

改变这一点。

 clieList.Add(new Client() //This is where the error is
            {
                Code = Convert.ToInt32(txt_cod.Text),
                Name = txt_name.Text,
                Phone = Convert.ToInt32(txt_phone.Text),
                Email = txt_email.Text,
            });

到此

 clieList.Add(new Client(Convert.ToInt32(txt_cod.Text),txt_name.Text,Convert.ToInt32(txt_phone.Text),txt_email.Text);

答案 2 :(得分:0)

您尝试创建Client实例而不向构造函数发送任何参数。

构造函数:

public Client (int code, string name, int phone, string email)

解决方案1:提供构造函数参数:

private void btn_add_Click(object sender, EventArgs e)
        {
            clieList.Add(new Client(Convert.ToInt32(txt_cod.Text), txt_name.Text, Convert.ToInt32(txt_phone.Text), Email = txt_email.Text,

解决方案2:添加无参数构造函数:

public Client()

并像以前一样打电话:

private void btn_add_Click(object sender, EventArgs e)
        {

            clieList.Add(new Client() //This is where the error is
            {
                Code = Convert.ToInt32(txt_cod.Text),
                Name = txt_name.Text,
                Phone = Convert.ToInt32(txt_phone.Text),
                Email = txt_email.Text,
            });

答案 3 :(得分:0)

这几乎就是错误所说的。 Client类没有默认(即无参数)构造函数。

此:

new Client
{
    Code = Convert.ToInt32(txt_cod.Text),
    Name = txt_name.Text,
    Phone = Convert.ToInt32(txt_phone.Text),
    Email = txt_email.Text,
}

这是语法糖:

var client = new Client();
client.Code = Convert.ToInt32(txt_cod.Text);
client.Name = txt_name.Text;
client.Phone = Convert.ToInt32(txt_phone.Text);
client.Email = txt_email.Text;

new Client()调用是使用默认构造函数的对象实例化,由于您定义了自定义构造函数,因此它不存在:如果您在类中没有定义构造函数,那么编译器将插入一个隐式默认构造函数,但是由于您定义了自定义的,因此不会隐式定义默认构造函数。

你必须这样做:

new Client(Convert.ToInt32(txt_cod.Text),
           txt_name.Text,
           Convert.ToInt32(txt_phone.Text),
           txt_email.Text)

或者在Client类中定义默认构造函数:

public Client()
{
}
相关问题