重构case select into if语句

时间:2013-08-28 21:40:56

标签: vb.net visual-studio-2010

我有以下代码:

    Dim useFont As Font = e.Font
    Dim myBrush As Brush = Brushes.Black
    ' Determine the font to draw each item based on    
    ' the index of the item to draw. 
    If e.Index = 0 Or 7 Or 10 Or 13 Then
        useFont = New Font(e.Font, FontStyle.Bold)
    Else
        useFont = DefaultFont
    End If

    ' Select e.Index
    '    Case 0
    'useFont = New Font(e.Font, FontStyle.Bold)
    '    Case 7
    'useFont = New Font(e.Font, FontStyle.Bold)
    '   Case 10
    'useFont = New Font(e.Font, FontStyle.Bold)
    '    Case 13
    'useFont = New Font(e.Font, FontStyle.Bold)
    'End Select

下面注释掉的代码是我旧的工作代码。我认为我最好重写它以使其更紧凑和可读,但是我无法弄清楚如何执行与case select相同的功能,因为它只会导致我的所有列表项变为粗体。我不知道我是否只是误解了OR运算符,而且OrElse也不起作用。

有人可以给我一些正确方向的指示吗?感谢。

1 个答案:

答案 0 :(得分:3)

If e.Index = 0 Or e.Index= 7 Or e.index=10 Or e.index=13 Then

但你可以同样做到

Case 0,7,10,13
相关问题