Best template for handling nested code conditions?

I have a web page that should selectively show or hide a significant number of controls. Each control appearance is determined based on which of the three conditions is true.

So, for example, given conditions A, B and C;

  • Control1 should only be displayed when A and B are true and C is false.
  • Control2 should only be displayed when B and C are true and A is false.
  • Control3 should only be visible when A and C are true, and it doesn't care that B.
  • etc.

If each control looked at each condition, the control logic would be ugly but doable.

If A Then
    If B Then
        If C Then

        Else

        End If
    Else
        If C Then

        Else

        End If
    End If
Else
    If B Then
        If C Then

        Else

        End If
    Else
        If C Then

        Else

        End If
    End If
End If

, , , 1 2 , . 26 :

A   False   B   Any     C   Any  
A   True    B   Any     C   Any  
A   Any     B   Any     C   False
A   False   B   Any     C   False
A   True    B   Any     C   False
A   Any     B   Any     C   True 
A   False   B   Any     C   True 
A   True    B   Any     C   True 
A   Any     B   False   C   Any  
A   False   B   False   C   Any  
A   True    B   False   C   Any  
A   Any     B   False   C   False
A   False   B   False   C   False
A   True    B   False   C   False
A   Any     B   False   C   True 
A   False   B   False   C   True 
A   True    B   False   C   True 
A   Any     B   True    C   Any  
A   False   B   True    C   Any  
A   True    B   True    C   Any  
A   Any     B   True    C   False
A   False   B   True    C   False
A   True    B   True    C   False
A   Any     B   True    C   True 
A   False   B   True    C   True 
A   True    B   True    C   True 

?

Edit: , , A, B C . :

Dim isMatch = Function(A As Boolean?, B As Boolean?, C As Boolean?) As Boolean
        Return (Not A.HasValue OrElse A.Value = SomeLongConditionA) _
        AndAlso (Not B.HasValue OrElse B.Value = SomeLongConditionB) _
        AndAlso (Not C.HasValue OrElse C.Value = SomeLongConditionC)
    End Function

Control1.Visible = isMatch(True, True, False)
Control2.Visible = isMatch(False, True, True)
Control3.Visible = isMatch(True, Nothing, True)
+3
2
Control1.Visible = A And B And Not C
Control2.Visible = Not A And B And C
Control3.Visible = A And C
...

, . : " ?"

EDIT:. A, B C , . :

Dim A As Boolean = SomeLongConditionA
Dim B As Boolean = SomeLongConditionB
Dim C As Boolean = SomeLongConditionC

Control1.Visible = A And B And Not C
...

, , , , .

+15

:

If A And B And Not C Then
    Control1.Visible = True
End If

If Not A And B And C Then
    Control2.Visible = True
End If

If A And C Then
    Control3.Visible = True
End If

: , , , .

+1

Source: https://habr.com/ru/post/1772020/


All Articles