83

VB.NET で、DateTime変数を「未設定」に設定する方法はありますか? また、 を に設定できるのにDateTimeNothingであるかどうかを確認できないのはNothingなぜですか? 例えば:

Dim d As DateTime = Nothing
Dim boolNotSet As Boolean = d Is Nothing 

2 番目のステートメントは、次のエラーをスローします。

'Is' operator does not accept operands of type 'Date'. Operands must be reference or
nullable types.
4

8 に答える 8

146

これは、VB.Net、IMOとの最大の混乱の原因の1つです。

NothingVB.Netの場合は、default(T)C#の場合と同等です。特定のタイプのデフォルト値です。

  • 値型の場合、これは基本的に「ゼロ」と同等です。for 、for 0、for 、..。IntegerFalseBooleanDateTime.MinValueDateTime
  • 参照型の場合、それはnull値です(まあ、何も参照しない参照)。

したがって、このステートメントは、明らかにコンパイルされない、とd Is Nothing同等です。d Is DateTime.MinValue

解決策:他の人が言っているように

  • どちらかを使用しますDateTime?(つまりNullable(Of DateTime))。これが私の好ましい解決策です。
  • または使用d = DateTime.MinValueまたは同等にd = Nothing

元のコードのコンテキストでは、次を使用できます。

Dim d As DateTime? = Nothing
Dim boolNotSet As Boolean = d.HasValue

より包括的な説明は、AnthonyD.Greenのブログにあります。

于 2011-05-03T13:06:11.373 に答える
14

DateTime は値型であるため、null にすることはできません。と等しいことを確認するか、代わりにDateTime.MinValue使用できますNullable(Of DateTime)

VB は、「役に立つ」ことで、そうではないことをしていると思わせることがあります。Date を Nothing に設定できる場合、実際には MinValue などの他の値に設定しています。

値型と参照型の広範な議論については、この質問を参照してください。

于 2011-05-03T12:49:31.633 に答える
5

DateTime は値型です。つまり、常に何らかの値を持つことを意味します。

これは整数のようなものです。0、1、または 0 未満になることはありますが、「何もない」ことはありません。

値 Nothing を取ることができる DateTime が必要な場合は、Nullable DateTime を使用します。

于 2011-05-03T12:50:22.500 に答える
4

DateTimenull許容値の操作に関するいくつかの例。

(詳細については、 Nullable Value Types(Visual Basic)を参照してください。)

'
' An ordinary DateTime declaration. It is *not* nullable. Setting it to
' 'Nothing' actually results in a non-null value.
'
Dim d1 As DateTime = Nothing
Console.WriteLine(String.Format("d1 = [{0}]\n", d1))
' Output:  d1 = [1/1/0001 12:00:00 AM]

' Console.WriteLine(String.Format("d1 is Nothing? [{0}]\n", (d1 Is Nothing)))
'
'   Compilation error on above expression '(d1 Is Nothing)':
'
'      'Is' operator does not accept operands of type 'Date'.
'       Operands must be reference or nullable types.

'
' Three different but equivalent ways to declare a DateTime
' nullable:
'
Dim d2? As DateTime = Nothing
Console.WriteLine(String.Format("d2 = [{0}][{1}]\n", d2, (d2 Is Nothing)))
' Output:  d2 = [][True]

Dim d3 As DateTime? = Nothing
Console.WriteLine(String.Format("d3 = [{0}][{1}]\n", d3, (d3 Is Nothing)))
' Output:  d3 = [][True]

Dim d4 As Nullable(Of DateTime) = Nothing
Console.WriteLine(String.Format("d4 = [{0}][{1}]\n", d4, (d4 Is Nothing)))
' Output:  d4 = [][True]

また、変数がnullかどうかを確認する方法について(Nothing(Visual Basic)から):

参照(またはnull許容値型)変数がnull= Nothingかどうかを確認するときは、またはを使用しないでください<> Nothing。常にIs Nothingまたはを使用してIsNot Nothingください。
于 2012-09-12T14:44:24.153 に答える
1

どのプログラミング言語でも、Null を使用するときは注意してください。上記の例は、別の問題を示しています。Nullable 型を使用する場合、その型からインスタンス化された変数は値 System.DBNull.Value を保持できることを意味します。「= Nothing」を使用して値をデフォルトに設定する解釈が変更されたわけでも、値のオブジェクトが 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

次に、オーバーライド可能なものを選択して選択し、必要なことを実行させることができます。多くの作業が必要ですが、本当に必要な場合は実行できます。

于 2012-04-10T15:11:55.930 に答える
1

以下を使用して簡単に確認することもできます。

If startDate <> Nothing Then
your logic
End If

DateTime データ型の startDate 変数が null かどうかをチェックします。

于 2014-12-30T12:18:13.227 に答える
1

これは以下のように確認できます:

if varDate = "#01/01/0001#" then
       '  blank date. do something.
else
       ' Date is not blank. Do some other thing
end if
于 2018-05-19T21:37:43.810 に答える
0

これを回避するには、代わりに Object データ型を使用します。

Private _myDate As Object
Private Property MyDate As Date
    Get
        If IsNothing(_myDate) Then Return Nothing
        Return CDate(_myDate)
    End Get
    Set(value As Date)
        If date = Nothing Then
            _myDate = Nothing
            Return
        End If
        _myDate = value
     End Set
End Property

次に、日付を次のように何も設定できません。

MyDate = Nothing
Dim theDate As Date = MyDate
If theDate = Nothing Then
    'date is nothing
End If
于 2013-07-14T12:55:07.193 に答える