2013-02-19 34 views
1

我試圖將輸入數據的驗證移入get; set;的類結構。get; set;與DateTime與.TryParse驗證?

public void PlotFiles() 
    { 

    List<DTVitem.item> dataitems; 
     DTVitem.item i; 
     DateTime.TryParse("2012/01/01", out i.dt); 
     DateTime.TryParse("04:04:04", out i.t); 
     int.TryParse("455", out i.v); 
     dataitems.Add(i); 

    } 

的結構也是在一個單獨的類(可能是不必要的)聲明:

public partial class DTVitem 
{ 
    public struct item 
    { 
     public DateTime dt; 
     public DateTime t; 
     public int v; 
    } 
} 

我每次設置DTVitem.item.dtDTVitem.item.t,或DTVitem.item.v,我希望它來執行相關.TryParse()要驗證的屬性內容。

然而,當我嘗試使用的TryParse()如下(試圖環繞this example from MSDN我的頭):

public partial class DTVitem 
{ 
    private DateTime _datevalue; 

    public string dt 
    { 
     get { return _datevalue; } 
     set { DateTime.TryParse(value, out _datevalue) ;} 
    } 
} 

我收到錯誤_datevalue is a DateTime and cannot be converted to a string。原因很明顯,在這種情況下返回路徑必須返回dt的類型(一個string)。但是,我試圖用幾種不同的方式來按摩這些東西,並且無法破解它。

將其設置爲結構實例的屬性時,如何實現驗證string值爲DateTime的目標?

正在使用set,因爲我正試圖以最好的方式?

我可以看到使用get; set;有很多的價值;進行驗證,並且真的很想了解它。

非常感謝,

馬特

[編輯]

感謝以下Jon Skeet您指出我的方式犯錯。

Here's another thread on problems with mutable structsand another speaking about instantiating a struct。注意structs are value types

我相信他指出的其餘部分只是認爲將結構化的方式埋在很遠的地方是沒有必要的,我應該回顧一下爲什麼要這樣做。

[解決方案]

我已經考慮了以下一些建議,並提出了以下幾點:

public partial class DTVitem 
{ 
    private DateTime _dtvalue, _tvalue; 
    private int _vvalue; 

    public string dt 
    { 
     get { return _dtvalue.ToString(); } 
     set { DateTime.TryParse(value, out _dtvalue); } 
    } 

    public string t 
    { 
     get { return _tvalue.ToString(); } 
     set { DateTime.TryParse(value, out _tvalue); } 
    } 

    public string v 
    { 
     get { return _vvalue.ToString(); } 
     set { int.TryParse(value, out _vvalue); } 
    } 
} 

裏面我的程序類,我實例化,並具有以下設置:

DTVitem item = new DTVitem(); 
item.dt = "2012/01/01"; 
item.t = "04:04:04"; 
item.v = "455"; 

所以我選擇不使用結構,而是一個類;或者真的是這個類的一個實例。

+6

這很不清楚爲什麼你在'DTVitem'中有一個公共字段作爲嵌套類型的公共可變結構。所有這些對我來說都是一個糟糕的主意。 – 2013-02-19 21:27:42

+2

你的錯誤是因爲你在'get'中返回一個'DateTime'來聲明一個被聲明爲'string'的屬性。 – juharr 2013-02-19 21:29:36

+1

Get和Set應該只能獲取並設置一個變量。這聽起來像一個類對你來說比一個結構更有用,至少是你使用它的方式。片段調用集應負責使用有效的DateTime而不是struct/class。 – 2013-02-19 21:30:53

回答

3

屬性只能有一種類型。如果您希望屬性是字符串類型的,那麼你就可以實現這樣說:

public partial class DTVitem 
{ 
    private DateTime _datevalue; 

    public string dt 
    { 
     get { return _datevalue.ToString(); } 
     set { DateTime.TryParse(value, out _datevalue) ;} 
    } 
} 

但是,使用的TryParse()將意味着,如果日期時間是無效的setter不會拋出異常。如果你想要這樣做,請改用DateTime.Parse()。

+0

謝謝我會檢查出這個可能性並回復並回復。 – mbrownnyc 2013-02-19 21:35:03

1
public partial class DTVitem 
{ 
    private DateTime _datevalue; 

    public string dt 
    { 
     get { return _datevalue.ToString(); } 
     set { DateTime.TryParse(value, out _datevalue) ;} 
    } 
} 
+0

謝謝我會檢查出這個可能性並回復並回復。 – mbrownnyc 2013-02-19 21:35:27

0

get; set;必須具有相同的類型。當你期待一個字符串時,你的get返回一個日期時間,因此錯誤。

只需使用顯式方法bool setDate(String datestring)並將代碼放在那裏。你可以從tryparse中返回一個bool來告訴你它是否成功。

1

你只是缺少get中的類型轉換。 _datevalue是DateTime,但是您的屬性是一個字符串。

get { return _datevalue.ToString(); } //or .toShortDateString() or ToShorttimeString() 
+0

感謝您指出這一套功能。 – mbrownnyc 2013-02-20 17:09:08

0

其他(設計居多)問題放在一邊,剛開回_datevalue作爲字符串的問題,你可以簡單地這樣做:

public string dt 
{ 
    get { return _datevalue.ToString(); } 
    set { if(!DateTime.TryParse(value, out _datevalue)) /* Error recovery!! */ ;} 
} 

>>>您可能還需要檢查DateTime.ToString()的文檔並查看訪問該屬性時想要獲取字符串的格式。