2014-10-18 96 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() 
{ 
} 
相關問題