Булева таблица във VB.Net - Изберете регистър, ако-друго или нещо друго?

Търся да опростя обработката на булев израз от три елемента. Изглежда, че „Избор на случай“ не предлага решение за тройни стойности, а операторът If изглежда малко прекалено. Ще работи по начина, по който го кодирах, но ако имате някакви идеи как да опростите това, ще съм благодарен за вашите прозрения. Ако не, надявам се този фрагмент да спести малко време на някой друг.

Public Sub SetThisFontStyle(ByRef theTextBox As RichTextBox, ByVal StyleToChange As String)

    'Get Initial Values:'
    Dim BoldState As Boolean = theTextBox.SelectionFont.Bold
    Dim ItalicState As Boolean = theTextBox.SelectionFont.Italic
    Dim UnderlineState As Boolean = theTextBox.SelectionFont.Underline

    'Find out what we re trying to change:'
    Select Case StyleToChange
        Case "Bold" : If BoldState Then BoldState = False Else BoldState = True
        Case "Italic" : If ItalicState Then ItalicState = False Else ItalicState = True
        Case "Underline" : If UnderlineState Then UnderlineState = False Else UnderlineState = True
        Case Else : Exit Sub
    End Select

    'Boolean Table just for reference:'
    '0 - 0 - 0 None'
    '1 - 0 - 0 Bold Only'
    '0 - 1 - 0 Italic Only'
    '0 - 0 - 1 Underline Only'
    '1 - 1 - 0 Bold and Italic'
    '0 - 1 - 1 Italic and Underline '
    '1 - 0 - 1 Bold and Underline'
    '1 - 1 - 1 Bold, Italic, and Underline'

    If Not BoldState And Not ItalicState And Not UnderlineState Then
        'Regular, without any styles'
        theTextBox.SelectionFont = _
        New Font(txtRichText.SelectionFont, Drawing.FontStyle.Regular)
    ElseIf BoldState And Not ItalicState And Not UnderlineState Then
        'Bold Only'
        theTextBox.SelectionFont = _
        New Font(txtRichText.SelectionFont, Drawing.FontStyle.Bold)
    ElseIf Not BoldState And ItalicState And UnderlineState Then
        'Italic Only'
        theTextBox.SelectionFont = _
        New Font(txtRichText.SelectionFont, Drawing.FontStyle.Italic)
    ElseIf Not BoldState And Not ItalicState And UnderlineState Then
        'Underline Only'
        theTextBox.SelectionFont = _
        New Font(txtRichText.SelectionFont, Drawing.FontStyle.Underline)
    ElseIf BoldState And ItalicState And Not UnderlineState Then
        'Bold and Italic'
        theTextBox.SelectionFont = _
        New Font(txtRichText.SelectionFont, Drawing.FontStyle.Bold + Drawing.FontStyle.Italic)
    ElseIf Not BoldState And ItalicState And UnderlineState Then
        'Italic and Underline'
        theTextBox.SelectionFont = _
        New Font(txtRichText.SelectionFont, Drawing.FontStyle.Italic + Drawing.FontStyle.Underline)
    ElseIf BoldState And Not ItalicState And UnderlineState Then
        'Bold and Underline'
        theTextBox.SelectionFont = _
        New Font(txtRichText.SelectionFont, Drawing.FontStyle.Bold + Drawing.FontStyle.Underline)
    ElseIf BoldState And ItalicState And UnderlineState Then
        'Bold, Italic, and Underline'
        theTextBox.SelectionFont = _
        New Font(txtRichText.SelectionFont, Drawing.FontStyle.Bold + Drawing.FontStyle.Italic + Drawing.FontStyle.Underline)
    Else
        Exit Sub
    End If

End Sub

person Lord Baddkitty    schedule 12.10.2010    source източник
comment
Можете да опитате да третирате трите резултата като трибитово число и да използвате израз за превключване (който може да изглежда по-грозен и да е по-малко поддържаем от това, което вече имате).   -  person Michael Todd    schedule 13.10.2010


Отговори (1)


Бих използвал Enum с атрибута [Flags] и след това можете да проверявате комбинации. Това също би направило кода по-четлив.

[Flags]
public enum TextSettingType
{
    None = 0x0,
    Bold = 0x1,
    Italic = 0x2,
    Underline = 0x3
}

Съжалявам за C#, не знам как да го преведа на VB.Net

person pstrjds    schedule 12.10.2010