vb.net - 是否可以在 VB.NET 中使用带有可为空值的 Select Case?

标签 vb.net

以下代码

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/

相关文章:

mysql - VB - SQL 致命内部连接错误 - 已关闭

.net - 简单线程速成类?

vb.net - 返回没有 Dim 的点作为点

javascript - 如何将脚本正确添加到控件?

javascript - 通过javascript更改iframe src时,整个页面刷新

c# - "Cannot convert lambda expression to type ' string ' because it is not a delegate type"在C#中查询数据集

.net - 从 vb.net 中的文件中提取值

vb.net - 在 Visual Basic 中使用变量名

wpf - 使用 XAML 基于另一个 ComboBox 填充 ComboBox

c# - 取消缩短网址