2012-03-21 65 views
-2

我總是在CN_中爲我的常量添加了前綴,如下所示,但我現在正在編碼爲接受的標準,我發現這些標準在本網站上鍊接。標準說我應該放棄我的常量CN_。因此,下面的例子中,如果我將CN_NetPrice更改爲NetPrice,我將與同名的method屬性發生衝突。顯然我不能這樣做,所以我留下了一個問題。我是否有命名約定問題,或者我的代碼一般存在問題?我有一個命名約定問題或代碼問題嗎?

public class TicketInformation 
{ 
    private const string CN_StartDate = "StartDate"; 
    private const string CN_EndDate = "EndDate"; 
    private const string CN_NetPrice = "NetPrice"; 
    private const string CN_NetTotalPrice = "NetTotalPrice"; 
    private const string CN_Tickets = "Tickets"; 

    public decimal NetPrice { get; set; } 
    public decimal NetTotalPrice { get; set; } 
    public decimal Tickets { get; set; } 

    public static TicketInformation Create(DateTime startDate, DateTime endDate) 
    { 
     try 
     { 
      TicketInformation ti = new TicketInformation(); 
      using (DataTable dt = DAC.ExecuteDataTable(
       "GetAllTicketInformationSelect", 
       DAC.Parameter(CN_StartDate, startDate), 
       DAC.Parameter(CN_EndDate, endDate))) 
      { 
       ti.NetTotalPrice = Convert.ToDecimal(dt.Rows[0][CN_NetTotalPrice]); 
       ti.NetPrice = Convert.ToDecimal(dt.Rows[0][CN_NetPrice]); 
       ti.Tickets = Convert.ToDecimal(dt.Rows[0][CN_Tickets]); 
      } 
      return ti; 
     } 
     catch (Exception ex) 

     { 
      throw new Exception(Convert.ToString(ex)); 
     } 
    } 

} 

}

+0

我會調用像'NetPriceColumn'這樣的常量。 – 2012-03-21 15:50:38

+0

您尚未提供任何有關您的實際問題的信息。你只給我們你認爲解決方案(2)可能是什麼。 – 2012-03-21 15:50:50

+0

我的問題是,如果我遵循命名約定,我的代碼將無法編譯,所以我不確定這是否是我不理解約定或使用我的代碼的結果。 – 2012-03-21 16:55:38

回答

4

你的常數不是其實代表淨價格(等)嗎?它代表淨價欄的名稱。所以我建議之一:

private const string StartDateColumn = "StartDate"; 
private const string EndDateColumn = "EndDate"; 
private const string NetPriceColumn = "NetPrice"; 
private const string NetTotalPriceColumn = "NetTotalPrice"; 
private const string TicketsColumn = "Tickets"; 

或者:

private static class Columns 
{ 
    internal const string StartDate = "StartDate"; 
    internal const string EndDate = "EndDate"; 
    internal const string NetPrice = "NetPrice"; 
    internal const string NetTotalPrice = "NetTotalPrice"; 
    internal const string Tickets = "Tickets"; 
} 

或者使用一個枚舉:

private enum Column 
{ 
    StartDate, EndDate, NetPrice, NetTotalPrice, Tickets; 
} 

...呼籲那些枚舉值之一ToString會給名。

0

你有一個命名規則的問題 - 列名的常量字符串應該從他們所代表的實際屬性不同的名稱。

5

我認爲這裏的問題只是一個名稱的選擇。舉個例子具有以下

private const string StartDate = "StartDate"; 

我的反饋消除CN_任何人提交,這將是你有一個壞名字的更改建議。這不是開始日期。而是我們在顯示信息時識別或標記開始日期的方式。如果應用此邏輯所有的常量的問題就會迎刃而解,我建議,而不是StartDateName

private const string StartDateName = "StartDate";