을 모든 프로그래밍 언어에주의해야합니다. 위의 예는 다른 문제를 보여줍니다. Nullable 형식을 사용하면 해당 형식에서 인스턴스화 된 변수가 System.DBNull 값을 보유 할 수 있음을 의미합니다.값; "= Nothing"을 사용하여 값을 기본값으로 설정하는 해석이 변경되었거나 값의 Object가 이제 null 참조를 지원할 수있는 것은 아닙니다. 그냥 경고 ... 행복한 코딩!
값 유형을 포함하는 별도의 클래스를 만들 수 있습니다. 그러한 클래스에서 생성 된 객체는 Nothing으로 할당 될 수있는 참조 유형입니다. 예 : 홈페이지에서
Public Class DateTimeNullable
Private _value As DateTime
'properties
Public Property Value() As DateTime
Get
Return _value
End Get
Set(ByVal value As DateTime)
_value = value
End Set
End Property
'constructors
Public Sub New()
Value = DateTime.MinValue
End Sub
Public Sub New(ByVal dt As DateTime)
Value = dt
End Sub
'overridables
Public Overrides Function ToString() As String
Return Value.ToString()
End Function
최종 클래스
'() :
Dim dtn As DateTimeNullable = Nothing
Dim strTest1 As String = "Falied"
Dim strTest2 As String = "Failed"
If dtn Is Nothing Then strTest1 = "Succeeded"
dtn = New DateTimeNullable(DateTime.Now)
If dtn Is Nothing Then strTest2 = "Succeeded"
Console.WriteLine("test1: " & strTest1)
Console.WriteLine("test2: " & strTest2)
Console.WriteLine(".ToString() = " & dtn.ToString())
Console.WriteLine(".Value.ToString() = " & dtn.Value.ToString())
Console.ReadKey()
' Output:
'test1: Succeeded()
'test2: Failed()
'.ToString() = 4/10/2012 11:28:10 AM
'.Value.ToString() = 4/10/2012 11:28:10 AM
그런 다음 당신이 선택하고 당신이 무엇을해야 할 overridables를 선택할 수 있습니다. 일이 많지만, 정말로 필요하다면 할 수 있습니다.
아래의 John Gant의 답변 외에도 datetime 변수 = Nothing인지 확인해 볼 수 있습니다 ("is"대신에 = 참고). – NYSystemsAnalyst
감사합니다, Dim boolNotSet As Boolean = d = 아무것도 가장 간단한 해결책처럼 보이지 않습니다. – Muleskinner
@Chris - 그가 VB를 사용하고 있다고 생각합니다. –