2015-12-28 62 views
-3

我發現有這麼多人問這個錯誤,但我可以找到一個解釋的正確答案。不包含帶1個參數「4」的構造函數

類別:Update

public class Log 
{ 

    public int id { get; set; } 
    public string description { get; set; } 
    public string type { get; set; } 
    public string action { get; set; } 
    public DateTime date { get; set; } 
    public string fullname { set; get; } 

    public Log() { 

    } 
    public Log(LogDs.LogFileRow row) 
    { 
     this.id = row.id; 
     this.type = row.type; 
     this.action = row.action.ToString(); 
     this.description = row.description.ToString(); 
     this.date = row.date; 
     this.fullname = row.fullname; 
    } 
} 

BC類:

public class LogBC 
{ 
    LogDalc dalc = new LogDalc(); 

    public List<Log> List(DateTime dateStart, DateTime dateEnd) 
    { 
     List<Log> listLog = new List<Log>(); 

     LogDs.LogFileDataTable dt = dalc.Sel(dateStart, dateEnd); 

     for (int i = 0; i < dt.Count; i++) 
     { 
      Log exp = new Log(dt[i]); 

      listLog.Add(exp); 
     } 

     return listLog; 
    } 

DALC類別:示於BC類

public class LogDalc: BaseDalc 
{ 

    public LogDs.LogFileDataTable Sel(DateTime dateStart, DateTime dateEnd) 
    { 
     LogDs ds = new LogDs(); 

     using (SqlConnection con = new SqlConnection(Global.Config.ConnStr)) 
     { 
      SqlCommand cmd = new SqlCommand("spp_txn_expenses_sel", con); 
      cmd.CommandType = CommandType.StoredProcedure; 

      cmd.Parameters.Add(new SqlParameter("@sdate", dateStart == DateTime.MinValue ? dbNull : dateStart)); 
      cmd.Parameters.Add(new SqlParameter("@edate", dateEnd == DateTime.MinValue ? dbNull : dateEnd)); 

      FillDataSet(cmd, ds, new string[] { ds.LogFile.TableName }); 
     } 

     return ds.LogFile; 
    } 

} 

誤差和精確地Log exp = new Log(dt[i]);。任何人都可以解釋爲什麼發生這種情況以及如何解決它?

+5

錯誤很簡單。您創建'Log'的新實例並在創建它時傳遞'dt [i]'。但是'Log'類中沒有接受參數的構造函數。 –

+0

不包含帶1個參數的構造函數。 – Valentin

+0

我用構造函數更新了我的代碼,但仍顯示相同的錯誤 – Saif

回答

0

Log類沒有構造函數接受單個參數。

提供的參數的構造函數:

public class Log 
{ 

    public int id { get; set; } 
    public string description { get; set; } 
    public string type { get; set; } 
    public string action { get; set; } 
    public DateTime date { get; set; } 
    public string fullname { set; get; } 

    public Log(object myValue) { 
     // your logic for myValue assignment to some property 
    } 
} 

或使用性質

Log exp = new Log() { Description = dt[i].ToString() }; 
+0

我添加了構造函數,但仍顯示錯誤 – Saif

+0

使您的構造函數參數的類型對象,看看它是否產生錯誤!我已經更新了代碼,如下所示 –

+0

它必須使用DataSet進行某些建議,正如您所看到的那樣'LogDs'是數據集 – Saif

0

Log類不知道如何處理你傳遞參數做的內聯分配。您需要定義一個構造函數,它具有這種參數:

public Log(LogDs.LogFileDataTable dt) 
{ 
    // initialize your settings from the DataTable here 
} 
相關問題