2012-01-05 82 views
0

我有以下班級。請注意,有些關鍵值未顯示:如何在更新課程時更新顯示文本長度的屬性?

namespace Storage.Models 
{ 
    public abstract class AuditableTable 
    { 
     public string Title { get; set; } 
     public string Text { get; set; } 
    } 

} 

我想將Text屬性的長度存儲在名爲TextLength的變量中。當創建類的實例或更新類時,是否可以自動執行此操作?

回答

1

你不一定需要的屬性,除非你想記錄初始值想要記錄初始長度,你可以這樣做:

string m_Text; 

    public string Text 
    { 
     get 
     { 
      return m_Text; 
     } 
     set 
     { 
      m_Text = value; 
      if (m_TextLength == 0) 
      { 
       m_TextLength = value.Length; 
      } 
     } 
    } 

    private int m_TextLength; 

    public int TextLength 
    { 
     get 
     { 
      return m_TextLength; 
     } 
    } 
3

你可以只添加屬性與getter:,

public int TextLength 
    { 
     get 
     { 
      return this.Text.Length; 
     } 
    } 

不過,如果你:

public abstract class AuditableTable 
{ 
    public string Title { get; set; } 
    public string Text { get; set; } 

    public int TextLength 
    { 
     get { return this.Text.Length; } 
    } 
} 
+0

我想運行包含長度列的報告。你認爲這樣做可以嗎?或者我應該在存儲數據時設置值? – 2012-01-05 03:44:49

+0

這樣做可能是可以的,除非你的報表引擎有一些奇怪的怪癖。 – Jacob 2012-01-05 03:56:13