Select Case True

后端 未结 9 1813
失恋的感觉
失恋的感觉 2020-11-27 07:08

Apparently this used to be a way in VB6 and VBA to short circuit and execute the first true case:

Select Case True
End Select

Is this still

9条回答
  •  鱼传尺愫
    2020-11-27 07:47

    This syntax is often used instead of an If...ElseIf statement. Some people find it a little easier to read. For example:

    Select Case True
        Case testVariable < 0
             Console.Write("You must supply a positive value.")
        Case testVariable > 10
             Console.Write("Please enter a number from 0-10.")
        Case True
             Call DoWork(testVariable)
    End Select
    

    The answer is that yes, this still works in VB.NET. Just take care with when you use it, because it's not a "standard programming construct" and may be unfamiliar to people that have to maintain your code in the future.

提交回复
热议问题