VB.Net Switch语句,包括每种情况下的代码段

时间:2018-05-17 14:09:26

标签: vb.net switch-statement

所以我正在VB.Net中编写一个程序,并且有一个关于Select / Case语句的简单问题,我似乎找不到答案。所以现在我的案例看起来像这样:

Select Case numColumns
    Case 0
        numColumns = numColumns + 1
        'Some code
    Case 1
        numColumns = numColumns + 1
        'Some different code
    Case 2
        numColumns = numColumns + 1
        'Some even more different code
    Case Else
        'Do Nothing
End Select

基本上,我需要在numColumns = numColumns + 1的每个案例中除{/ 1>}以外的Case Else语句。有没有办法在不向每个Case语句添加相同代码行的情况下执行此操作?

我能想到的简单解决方案是在我的select之前可能有一个if语句,然后在那里做增量,但我想知道是否有办法像Case All行那样做

3 个答案:

答案 0 :(得分:3)

这应该有效:

Select Case numColumns
    Case 0 To 2
        numColumns = numColumns + 1
        'Some code
    Case Else
        'Do Nothing
End Select

修改

如果你认为它比Select/Case语句更美观,你可以使用额外的If来解决它:

Select Case numColumns
    Case 0 To 2
        numColumns = numColumns + 1

        Select Case numColumns
            Case 0
                'Some code
            Case 1
                'Some different code
            Case 2
                'Some even more different code
        End Select
    Case Else
        'Do Nothing
End Select

如果你真的"什么都不做"在Else - 部分中,您可以完全省略它。

答案 1 :(得分:3)

您可以尝试使用增量

启动它
numColumns = numColumns + 1
Select Case numColumns - 1
    Case 0
        'Some code
    Case 1
        'Some different code
    Case 2
        'Some even more different code
    Case Else
        numColumns = numColumns - 1
        'Do Nothing
End Select

答案 2 :(得分:0)

另一个例子。

Private _numColumns As Integer

Public Property NumColumns As Integer
Get
    Return _numColumns 
End Get
Private Set(value As Integer)

    _numColumns = value

    '**************************************
    'NOTE: You could place your Select Case statement here (I don't recommend doing this though).
    'Maybe add logic to see if "value" is less than or greater a "_minColumn" and "_maxColumn" constant before assigning it (constants are not declared in this sample).
    '**************************************

End Set
End Property

Private Sub IncrementNumColumns()
    NumColumns += 1
End Sub

'Not currently used -- Here as an example. 
Private Sub DecrementNumColumns()
    NumColumns -= 1
End Sub

Private Function CodeSharedByAllColumnFunctions(p_myParam1 As Object, p_myParam2 As Object) As Integer
    IncrementNumColumns()
    'Maybe return some other result here. Otherwise change this to a Sub()
    'If nothing in Process1Column(), Process2Column(), and Process3Column() is reusable, then -
    ' add the IncrementNumColumns() to each method.
End Function

Private Sub Process1Column(p_myParam1 As Object, p_myParam2 As Object)
   Dim _someReusableCalculationResult = CodeSharedByAllColumnFunctions(p_myParam1, p_myParam2)
  'Some more code that unique to a single column
End Sub

Private Sub Process2Column(p_myParam1 As Object, p_myParam2 As Object)
     Dim _someReusableCalculationResult = CodeSharedByAllColumnFunctions(p_myParam1, p_myParam2)
    'Some more code that unique to 2 columns
End Sub

Private Sub Process3Column(p_myParam1 As Object, p_myParam2 As Object)
     Dim _someReusableCalculationResult = CodeSharedByAllColumnFunctions(p_myParam1, p_myParam2)
    'Some more code that unique to 3 columns
End Sub

Private Sub ProcessNumColumns() 
    Dim _someValue1, _someValue2 As Object
    _someValue1 = New With {.something = "abc"}
    _someValue2 = New With {.something = "123"}

Private Sub ProcessNumColumns() 
    Dim _someValue1, _someValue2 As Object
    _someValue1 = New With {.something = "abc"}
    _someValue2 = New With {.something = "123"}

    'Personally I wouldn't use a Select case here. I would use reflection to determine the correct method to call. 
    'I think that's outside of the scope of your question though.
    '
    'For the sake of readability, please do your best to keep the code within each Case to a minimum.
    'If possible, PLEASE avoid nested Select Case statements.
    Select Case NumColumns
        Case 0
            Process1Column(_someValue1, _someValue2)
        Case 1
            Process2Column(_someValue1, _someValue2)
        Case 2
            Process3Column(_someValue1, _someValue2)
    End Select
End Sub