以下代码
Sub Foo(i As Int32?)
Select Case i
Case Nothing ' <-- warning here
' Do something
Case 0
' Do something else
Case Else
' Do something different
End Select
End Sub
产生以下警告:
但是,Warning BC42037: This expression will always evaluate to Nothing (due to null propagation from the equals operator). To check if the value is null consider using 'Is Nothing'.
Case Is Nothing
会产生语法错误:
Error BC30239: Relational operator expected.
有没有办法将 Select Case
与可为空值类型和 Nothing
情况的 case 子句结合使用?
最佳答案
这是我目前使用的解决方法。我期待其他在多个 Case
子句的情况下减少重复的解决方案:
Select Case True
Case i Is Nothing
' Do something
Case i = 0
' Do something else
Case Else
' Do something different
End Select
关于vb.net - 是否可以在 VB.NET 中使用带有可为空值的 Select Case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43323298/