2016-10-13 8 views
0
Sub Main() 
    Dim e As Example = New Example() 
    ' Set property. 
    If Date.TryParse("Null", e.Number) Then 
     'get the valid date 
     '10/12/2016' 
    Else 
     'get the value as nothing 
     'Nothing 
    End If 
    ' Get property. 
    Console.WriteLine(e.Number) 
    Console.ReadKey() 
End Sub 

Class Example 
    Private _count As Date 

    Public Property Number() As Date 
     Get 
      Return _count 
     End Get 
     Set(ByVal value As Date) 
      _count = value 
     End Set 
    End Property 
End Class 

は、私が正しい日付を取得したいの内部e.number 注: - 私はe.numberNULL可能日時は

で値を取得する必要があります

答えて

1

あなたのプロパティをnullableにする必要があります。つまり、Nullable(Of Date)または略号Date?を使用することを意味します。それがNULL可能値の型を使用する時間が来るとき、あなたは最初に、例えば、そのValueプロパティからその値を取得し、その後、使用する値があるのか​​どうかを判断するために、そのHasValueプロパティを使用する必要があります

If Date.TryParse(myString, myDate) Then 
    myObject.MyNullableDateProperty = myDate 
Else 
    myObject.MyNullableDateProperty = Nothing 
End If 

:あなたはその後、これを行うことができます

If myObject.MyNullableDateProperty.HasValue Then 
    Console.WriteLine("The date is " & myObject.MyNullableDateProperty.Value.ToString()) 
Else 
    Console.WriteLine("There is no date") 
End If 
関連する問題