2016-10-13 2 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?을 사용하는 것을 의미합니다. 그런 다음이 작업을 수행 할 수 있습니다

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

를가 먼저 사용하는 값이 있는지 여부를 결정하기 위해 HasValue 속성을 사용해야합니다 널 (NULL) 값 유형을 사용하는 시간이 온다 때, 예를 들어, 그 Value 특성에서 그 가치를

If myObject.MyNullableDateProperty.HasValue Then 
    Console.WriteLine("The date is " & myObject.MyNullableDateProperty.Value.ToString()) 
Else 
    Console.WriteLine("There is no date") 
End If