2009-06-09 55 views
15

簡單的問題,希望一個簡單的答案:訪問器與不同的設置和獲取類型?

我想做到以下幾點:

private DateTime m_internalDateTime; 
public var DateTimeProperty 
{ 
    get { return m_internalDateTime.ToString(); } // Return a string 
    set { m_internalDateTime = value; } // here value is of type DateTime 
} 

以上僅僅是我想要做的一個例子。我想有一個公共的訪問器到一個類型爲x的內部變量。我希望將該變量作爲字符串獲取,但使用x類型的東西進行設置。

這可能嗎?

- 編輯 -

我才意識到我可以做這樣的事情:

private DateTime m_internalDateTime; 
public object DateTimeProperty 
{ 
    get { return m_internalDateTime.ToString(); } // Return a string 
    set { m_internalDateTime = (DateTime)value; } // here value is of type DateTime 
} 

不過,讓說我使用的類型Y的不是「字符串」作爲我的「得到」型。如果我想在我的代碼中使用「DateTimeProperty」,那麼我必須投射它。

+1

我強烈建議您在您的編輯不使用的代碼。除了常規的一個奇怪的突破,這可能會有輕微的表現和嚴重的本地化問題。 – 2009-06-09 19:24:10

+8

你不敢寫這段代碼嗎? – mquander 2009-06-09 20:59:12

回答

8

號可以很明顯的添加的ToString()的調用代碼,但你不能做你建議沒有什麼不同的名字是這樣的:

private DateTime m_internalDateTime; 
public DateTime SetDateTime { set { m_internalDateTime = value; } } 
public string GetDateTime { get { return m_internalDateTime.ToString(); } } 

,或者甚至更好至使用方法而不是屬性(如在評論中所指出):

private DateTime m_internalDateTime; 
public void SetDateTime(DateTime dateTime) { m_internalDateTime = dateTime; } 
public string GetDateTime() { return m_internalDateTime.ToString(); } 

記住var隱含,編譯時鍵入var iables,而不是動態變量。

肯定不要做你在編輯中注意到的。它引入了慣例中的休息,可能的性能影響(儘管很輕微),以及顯着的本地化問題。

+9

如果你打算使用GetDateTime和SetDateTime,它們應該是方法而不是屬性。 – 2009-06-09 18:37:37

+0

我可以問爲什麼要做這些方法而不是屬性? – Nick 2009-06-09 18:44:12

+8

@Nick:因爲方法就像對某個對象做某事的動詞,而屬性就像是對該對象所說的某些事物的名詞一樣。 SetDateTime和GetDateTime是動詞,因此應該是方法。 – 2009-06-09 18:47:38

5

作爲一個財產,這是不可能的。您可以使Get和Set方法具有不同的類型,但對於屬性,類型必須相同。

編輯:

雖然:

private DateTime m_internalDateTime; 
public object DateTimeProperty 
{ 
    get { return m_internalDateTime.ToString(); } // Return a string 
    set { m_internalDateTime = (DateTime)value; } // here value is of type DateTime 
} 

語法正確,將編譯並允許您接受時間日期作爲輸入,並返回一個字符串,這不會是一個很好的計劃。它的工作原理,但它使你和任何人訪問這個代碼,執行不需要的驗證。另外,它在未來對另一個開發人員很脆弱,不知道或意識到隱含的規則,因此您已經失去了編譯時安全性。此外,它幾乎沒有更多的代碼來創建兩個屬性,或者兩個方法來實現相同的目標,以強類型的方式。個人而言,我會推薦使用兩種方法(請參閱傑夫耶茨的評論爲什麼好的解釋)。

private DateTime m_internalDateTime; 
public string GetDateTime() 
{ 
    return m_internalDateTime.ToString(); 
} 

public void SetDateTime(DateTime dateTime) 
{ 
    m_internalDateTime = dateTime; 
} 
3

不是這樣的,但可以肯定的是訪問m_internalDateTime場的第二屬性。

public string DateTimeString 
{ 
    get { return m_internalDateTime.ToString(); } 
} 
0

簡單的回答否,對於您的外部代碼,您的財產將按照字段的確切方式行事,您無法擁有屬性具有不同的設置/獲取類型,就像您無法將字段設置爲鍵入,當你請求它的值時,返回一個不同的類型。

0

怎麼樣:

private DateTime intDT; 
public string DateTimeProperty 
{ 
     get { return intDT.ToString(); } // Return a string 
     set 
     { 
     DateTime dt; 
     if (DateTime.TryParse(value, out dt)) 
      intDT = dt; 
     else throw new ArgumentException(string.Format(
      "{0} cannot be converted to a DateTime.", value);   
     } 
} 
3

也許這有助於

public class TDecimal 
{ 
    private decimal? m_value; 
    public bool HasValue { get { return m_value.HasValue; } } 
    public decimal Value { get { return m_value.Value; } } 

    public static implicit operator TDecimal(string a_value) 
    { 
     decimal d; 
     if (decimal.TryParse(a_value, out d)) 
     { 
      return new TDecimal() {m_value = d}; 
     } 

     return new TDecimal() {m_value = null}; 
    } 

    public static implicit operator decimal(TDecimal a_value) 
    { 
     if(a_value.HasValue) 
     { 
      return a_value.Value; 
     } 

     throw new ArgumentNullException("a_value"); 
    } 
} 

public class A 
{ 
    public TDecimal Prop { get; set; } 
} 


A a = new A(); 

a.Prop = "123"; 
if (a.Prop.HasValue) 
{ 
    decimal d = a.Prop; 
}