2014-10-18 119 views
-2

我很新的C#,我不明白我在做什么错了,这里是我的代码:C#错误:不包含一个构造函数参数0

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

此代码起源从以前的问题,我问我如何加载我的列表数据到我的文本框。

+1

你的客户端类需要4个参数,但要与'新客户创建()'尝试'新的客户端(Convert.ToInt32(txt_cod.Text,txt_name.Text ........)'阅读一些关于c#的文档可能会有所帮助.... – 2014-10-18 19:49:14

回答

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)); 
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, 
      }); 
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); 
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() 
{ 
} 
相关问题