2012-02-24 39 views
0

我有下面的代碼的問題:獲得一個「爲最佳重載的方法匹配......有一些無效參數」的錯誤消息

public class ClientGroupDetails 
{ 
    public DateTime Col2; 
    public String Col3; 
    public Int32 Col4; 

    public ClientGroupDetails(DateTime m_Col2, String m_Col3, Int32 m_Col4) 
    { 
     Col2 = m_Col2; 
     Col3 = m_Col3; 
     Col4 = m_Col4; 
    } 

    public ClientGroupDetails() { } 
} 

[WebMethod()] 
public List<ClientGroupDetails> GetClientGroupDetails(string phrase) 
{ 
    var client_group_details = new List<ClientGroupDetails>(); 

    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"])) 
    { 
     using (command = new SqlCommand(@"select col2, col3, col4 from table1 where col1 = @strSearch", connection)) 
     { 
      command.Parameters.Add("@strSearch", SqlDbType.VarChar, 255).Value = phrase; 

      connection.Open(); 
      using (reader = command.ExecuteReader()) 
      { 
       int Col2Index = reader.GetOrdinal("col2"); 
       int Col3Index = reader.GetOrdinal("col3"); 
       int Col4Index = reader.GetOrdinal("col4"); 

       while (reader.Read()) 
       { 
        client_group_details.Add(new ClientGroupDetails(
         reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null : (Nullable<DateTime>)reader.GetDateTime(Col2Index), 
         reader.IsDBNull(Col3Index) ? null : reader.GetString(Col3Index), 
         reader.GetInt32(Col4Index))); 
       } 
      } 
     } 
    } 

    return client_group_details; 
} 
} 

這是給我下面的錯誤:

Compiler Error Message: CS1502: The best overloaded method match for 'Conflicts.ClientGroupDetails.ClientGroupDetails(System.DateTime, string, int)' has some invalid arguments 
Line 184: client_group_details.Add(new ClientGroupDetails(
+4

(無聲無息地公開字段,帶'm_'前綴的參數等) – 2012-02-24 12:39:07

+0

@Marc Grav呃,爲什麼? – oshirowanen 2012-02-24 13:00:58

+0

公共領域從來都不是一個好主意,並且不會對屬性賦予任何幫助,從而使您能夠安全地稍後調整實現(並且完全不需要任何費用)。那麼'm_'告訴你,你還不知道?實際上,大多數人喜歡這樣的符號使用'm_'作爲**字段**,而不是參數 – 2012-02-24 13:17:03

回答

2

使m_Col2 DateTime?Nullable<DateTime>

所以最後它會像這樣

public class ClientGroupDetails 
{ 
    public Nullable<DateTime> Col2; 
    public String Col3; 
    public Int32 Col4; 

    public ClientGroupDetails(Nullable<DateTime> m_Col2, String m_Col3, Int32 m_Col4) 
    { 
     Col2 = m_Col2; 
     Col3 = m_Col3; 
     Col4 = m_Col4; 
    } 

    public ClientGroupDetails() { } 
} 

[WebMethod()] 
public List<ClientGroupDetails> GetClientGroupDetails(string phrase) 
{ 
    var client_group_details = new List<ClientGroupDetails>(); 
    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"])) 
    { 
     using (command = new SqlCommand(@"select col2, col3, col4 from table1 where col1 = @strSearch", connection)) 
     { 
      command.Parameters.Add("@strSearch", SqlDbType.VarChar, 255).Value = phrase; 

      connection.Open(); 
      using (reader = command.ExecuteReader()) 
      { 
       int Col2Index = reader.GetOrdinal("col2"); 
       int Col3Index = reader.GetOrdinal("col3"); 
       int Col4Index = reader.GetOrdinal("col4"); 

       while (reader.Read()) 
       { 
        client_group_details.Add(new ClientGroupDetails(
         reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null : (Nullable<DateTime>)reader.GetDateTime(Col2Index), 
         reader.IsDBNull(Col3Index) ? (string)null : reader.GetString(Col3Index), 
         reader.GetInt32(Col4Index))); 
       } 
      } 
     } 
    } 

    return client_group_details; 
} 
} 

BTW:我會建議你在POCO使用屬性,而不是公共實例變量,並給他們有意義的名字2.使用小寫的局部變量名

+0

完成,但我現在正在編譯錯誤消息:CS1502:最好的重載方法匹配'ClientGroupDetails.ClientGroupDetails(System.DateTime? ,string,int)'有一些無效參數' – oshirowanen 2012-02-24 12:44:22

+0

您是否嘗試將null轉換爲第二個參數的字符串? – kingpin 2012-02-24 13:02:44

1

的構造函數簽名是DataTime

new ClientGroupDetails(
      reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null 
     ... 
:和你 Nullable<DataTime>

public ClientGroupDetails(DateTime m_Col2, String m_Col3, Int32 m_Col4) 

稱它爲你有把它稱爲

更改構造函數來:

public ClientGroupDetails(DateTime? m_Col2, string m_Col3, int m_Col4) 

和物業:

public DateTime? Col2; 

注:

  • 你不必寫的Int32,int是一個「別名「 爲了它。
  • Nullable<DateTime> =>DataTime?相同的是它的別名。
+0

這給了我以下錯誤消息:編譯器錯誤消息:CS0266:不能隱式轉換類型'System.DateTime?'到'System.DateTime'。存在明確的轉換(你是否缺少演員?)' – oshirowanen 2012-02-24 12:40:49

+0

@oshirowanen。您還需要更改該屬性。 '公共DateTime? Col1;' – gdoron 2012-02-24 12:42:21

+0

完成,但我現在正在編譯錯誤消息:CS1502:'ClientGroupDetails.ClientGroupDetails(System.DateTime?,string,int)'的最佳重載方法匹配'有一些無效參數' – oshirowanen 2012-02-24 12:44:43

2

ClientGroupDetails的構造不採取Nullable<DateTime>(又名DateTime?) - 它需要平DateTime。您需要更改ClientGroupDetails使用的內容,或者考慮默認值(可能是DateTime.MinValue)。

例如::

client_group_details.Add(new ClientGroupDetails(
    reader.IsDBNull(Col2Index) ? DateTime.MinValue : reader.GetDateTime(Col2Index), 
    reader.IsDBNull(Col3Index) ? null : reader.GetString(Col3Index), 
    reader.GetInt32(Col4Index))); 

或者保留現有的閱讀器的代碼,並更改POCO:

public class ClientGroupDetails 
{ 
    public DateTime? Col2; 
    ...  
    public ClientGroupDetails(DateTime? m_Col2, ... 
    { 
     Col2 = m_Col2; 
     ... 
    } 
    ... 
} 

或者 - 使用一個工具,如「短小精悍」,這將做這一切爲你:

var list = connection.Query<ClientGroupDetails>(
     @"select col2, col3, col4 from table1 where col1 = @phrase", 
     new {phrase}).ToList(); 
+0

改變了POCO,現在我得到了'編譯器錯誤信息:CS1502:'ClientGroupDetails.ClientGroupDetails(System.DateTime?,string,int)'的最佳重載方法匹配'有一些無效參數' – oshirowanen 2012-02-24 12:45:20

+0

@oshirowanen你有沒有試過將它分成兩個步驟?即DateTime? x = ...;字符串y = ...; int z = ...; var obj = new ClientGroupDetails(x,y,z); client_group_details.Add(OBJ); ?這將有助於識別錯誤 – 2012-02-24 12:49:34

相關問題