Nullable(Of T) の null 値は何ですか?
質問
null 許容プロパティがあり、null 値を返したいと考えています。VB.NET でそれを行うにはどうすればよいですか?
現在このソリューションを使用していますが、もっと良い方法があるかもしれないと思います。
Public Shared ReadOnly Property rubrique_id() As Nullable(Of Integer)
Get
If Current.Request.QueryString("rid") <> "" Then
Return CInt(Current.Request.QueryString("rid"))
Else
Return (New Nullable(Of Integer)).Value
End If
End Get
End Property
解決
「何もない」というキーワードをお探しですか?
他のヒント
はい、VB.NET では Nothing、C# では null です。
Nullable ジェネリック データ型により、コンパイラは値型に "Nothing" (または null) 値を割り当てることができます。明示的に書かないと実行できません。
Public Shared ReadOnly Property rubrique_id() As Nullable(Of Integer)
Get
If Current.Request.QueryString("rid") <> "" Then
Return CInt(Current.Request.QueryString("rid"))
Else
Return Nothing
End If
End Get
End Property
または、これは私が使用している方法です、正直に言うと、ReSharperが私に教えてくれました:)
finder.Advisor = ucEstateFinder.Advisor == "-1" ? (long?)null : long.Parse(ucEstateFinder.Advisor);
上記の代入では、null を finder.Advisor*(long?)* に直接代入しても問題はありません。しかし、if句を使用しようとすると、そのようにキャストする必要があります (long?)null
.
それでも Nothing
使用できる場合、「既存の」コードはほぼ正しいです。ただ取得しようとしないでください .Value
:
Public Shared ReadOnly Property rubrique_id() As Nullable(Of Integer)
Get
If Current.Request.QueryString("rid") <> "" Then
Return CInt(Current.Request.QueryString("rid"))
Else
Return New Nullable(Of Integer)
End If
End Get
End Property
これは、たまたまそれを削減したい場合の最も簡単な解決策になります。 If
表現:
Public Shared ReadOnly Property rubrique_id() As Nullable(Of Integer)
Get
Return If(Current.Request.QueryString("rid") <> "", _
CInt(Current.Request.QueryString("rid")), _
New Nullable(Of Integer))
End Get
End Property
所属していません StackOverflow