Skip to main content
Tweeted twitter.com/StackCodeReview/status/691691568575746048
typo made code uncompilable
Source Link
Mathieu Guindon
  • 75.1k
  • 18
  • 191
  • 463

This approach was born out of wanting to pass a logical expression (E.G. ">=10000") as a parameter to a function.

So, I built an Enum and a function to use it to evaluate logical expressions.

Is this a good, maintainable, scalable approach?

Public Enum comparisonOperator
    '/ Used [[ ]] because the outer set escape the inner brackets for the Enum list, which will then escape the operator when chosen as a value. E.G. they will appear as "Operator = [<]" in code.
    [[<]] = 1
    [[<=]] = 2
    [[=]] = 3
    [[>=]] = 4
    [[>=]][[>]] = 5
End Enum

Public Function ComparisonIsTrue(ByVal sourceValue As Variant, ByVal operator As comparisonOperator, ByVal comparisonValue As Variant) As Boolean

    Dim isTrue As Boolean
    
    Select Case operator
    
        Case Is = 1 '/ <
        isTrue = (sourceValue < comparisonValue)
        
        Case Is = 2 '/ <=
        isTrue = (sourceValue <= comparisonValue)
        
        Case Is = 3 '/ =
        isTrue = (sourceValue = comparisonValue)
        
        Case Is = 4 '/ >=
        isTrue = (sourceValue >= comparisonValue)
        
        Case Is = 5 '/ >
        isTrue = (sourceValue > comparisonValue)
        
        Case Else
        '/ Error Handling
        Stop
    
    End Select
    
    ComparisonIsTrue = isTrue

End Function

This approach was born out of wanting to pass a logical expression (E.G. ">=10000") as a parameter to a function.

So, I built an Enum and a function to use it to evaluate logical expressions.

Is this a good, maintainable, scalable approach?

Public Enum comparisonOperator
    '/ Used [[ ]] because the outer set escape the inner brackets for the Enum list, which will then escape the operator when chosen as a value. E.G. they will appear as "Operator = [<]" in code.
    [[<]] = 1
    [[<=]] = 2
    [[=]] = 3
    [[>=]] = 4
    [[>=]] = 5
End Enum

Public Function ComparisonIsTrue(ByVal sourceValue As Variant, ByVal operator As comparisonOperator, ByVal comparisonValue As Variant) As Boolean

    Dim isTrue As Boolean
    
    Select Case operator
    
        Case Is = 1 '/ <
        isTrue = (sourceValue < comparisonValue)
        
        Case Is = 2 '/ <=
        isTrue = (sourceValue <= comparisonValue)
        
        Case Is = 3 '/ =
        isTrue = (sourceValue = comparisonValue)
        
        Case Is = 4 '/ >=
        isTrue = (sourceValue >= comparisonValue)
        
        Case Is = 5 '/ >
        isTrue = (sourceValue > comparisonValue)
        
        Case Else
        '/ Error Handling
        Stop
    
    End Select
    
    ComparisonIsTrue = isTrue

End Function

This approach was born out of wanting to pass a logical expression (E.G. ">=10000") as a parameter to a function.

So, I built an Enum and a function to use it to evaluate logical expressions.

Is this a good, maintainable, scalable approach?

Public Enum comparisonOperator
    '/ Used [[ ]] because the outer set escape the inner brackets for the Enum list, which will then escape the operator when chosen as a value. E.G. they will appear as "Operator = [<]" in code.
    [[<]] = 1
    [[<=]] = 2
    [[=]] = 3
    [[>=]] = 4
    [[>]] = 5
End Enum

Public Function ComparisonIsTrue(ByVal sourceValue As Variant, ByVal operator As comparisonOperator, ByVal comparisonValue As Variant) As Boolean

    Dim isTrue As Boolean
    
    Select Case operator
    
        Case Is = 1 '/ <
        isTrue = (sourceValue < comparisonValue)
        
        Case Is = 2 '/ <=
        isTrue = (sourceValue <= comparisonValue)
        
        Case Is = 3 '/ =
        isTrue = (sourceValue = comparisonValue)
        
        Case Is = 4 '/ >=
        isTrue = (sourceValue >= comparisonValue)
        
        Case Is = 5 '/ >
        isTrue = (sourceValue > comparisonValue)
        
        Case Else
        '/ Error Handling
        Stop
    
    End Select
    
    ComparisonIsTrue = isTrue

End Function
added 30 characters in body
Source Link
Kaz
  • 8.8k
  • 2
  • 30
  • 68

This approach was born out of wanting to pass a logical expression (E.G. ">=10000") as a parameter to a function.

So, I built an Enum and a function to use it to evaluate logical expressions.

Is this a good, maintainable, scalable approach?

Public Enum comparisonOperator
    '/ Used [[ ]] because the outer set escape the inner brackets for the Enum list, which will then escape the operator when chosen as a value. E.G. they will appear as "Operator = [<]" in code.
    [[<]] = 1
    [[<=]] = 2
    [[=]] = 3
    [[>=]] = 4
    [[>=]] = 5
End Enum

Public Function ComparisonIsTrue(ByVal sourceValue As Variant, ByVal operator As comparisonOperator, ByVal comparisonValue As Variant) As Boolean

    Dim isTrue As Boolean
    
    Select Case operator
    
        Case Is = 1 '/ <
        isTrue = (ValuesourceValue < comparisonValue)
        
        Case Is = 2 '/ <=
        isTrue = (ValuesourceValue <= comparisonValue)
        
        Case Is = 3 '/ =
        isTrue = (ValuesourceValue = comparisonValue)
        
        Case Is = 4 '/ >=
        isTrue = (ValuesourceValue >= comparisonValue)
        
        Case Is = 5 '/ >
        isTrue = (ValuesourceValue > comparisonValue)
        
        Case Else
        '/ Error Handling
        Stop
    
    End Select
    
    ComparisonIsTrue = isTrue

End Function

This approach was born out of wanting to pass a logical expression (E.G. ">=10000") as a parameter to a function.

So, I built an Enum and a function to use it to evaluate logical expressions.

Is this a good, maintainable, scalable approach?

Public Enum comparisonOperator
    '/ Used [[ ]] because the outer set escape the inner brackets for the Enum list, which will then escape the operator when chosen as a value. E.G. they will appear as "Operator = [<]" in code.
    [[<]] = 1
    [[<=]] = 2
    [[=]] = 3
    [[>=]] = 4
    [[>=]] = 5
End Enum

Public Function ComparisonIsTrue(ByVal sourceValue As Variant, ByVal operator As comparisonOperator, ByVal comparisonValue As Variant) As Boolean

    Dim isTrue As Boolean
    
    Select Case operator
    
        Case Is = 1 '/ <
        isTrue = (Value < comparisonValue)
        
        Case Is = 2 '/ <=
        isTrue = (Value <= comparisonValue)
        
        Case Is = 3 '/ =
        isTrue = (Value = comparisonValue)
        
        Case Is = 4 '/ >=
        isTrue = (Value >= comparisonValue)
        
        Case Is = 5 '/ >
        isTrue = (Value > comparisonValue)
        
        Case Else
        '/ Error Handling
        Stop
    
    End Select
    
    ComparisonIsTrue = isTrue

End Function

This approach was born out of wanting to pass a logical expression (E.G. ">=10000") as a parameter to a function.

So, I built an Enum and a function to use it to evaluate logical expressions.

Is this a good, maintainable, scalable approach?

Public Enum comparisonOperator
    '/ Used [[ ]] because the outer set escape the inner brackets for the Enum list, which will then escape the operator when chosen as a value. E.G. they will appear as "Operator = [<]" in code.
    [[<]] = 1
    [[<=]] = 2
    [[=]] = 3
    [[>=]] = 4
    [[>=]] = 5
End Enum

Public Function ComparisonIsTrue(ByVal sourceValue As Variant, ByVal operator As comparisonOperator, ByVal comparisonValue As Variant) As Boolean

    Dim isTrue As Boolean
    
    Select Case operator
    
        Case Is = 1 '/ <
        isTrue = (sourceValue < comparisonValue)
        
        Case Is = 2 '/ <=
        isTrue = (sourceValue <= comparisonValue)
        
        Case Is = 3 '/ =
        isTrue = (sourceValue = comparisonValue)
        
        Case Is = 4 '/ >=
        isTrue = (sourceValue >= comparisonValue)
        
        Case Is = 5 '/ >
        isTrue = (sourceValue > comparisonValue)
        
        Case Else
        '/ Error Handling
        Stop
    
    End Select
    
    ComparisonIsTrue = isTrue

End Function
Source Link
Kaz
  • 8.8k
  • 2
  • 30
  • 68

A global Enum for comparison operators

This approach was born out of wanting to pass a logical expression (E.G. ">=10000") as a parameter to a function.

So, I built an Enum and a function to use it to evaluate logical expressions.

Is this a good, maintainable, scalable approach?

Public Enum comparisonOperator
    '/ Used [[ ]] because the outer set escape the inner brackets for the Enum list, which will then escape the operator when chosen as a value. E.G. they will appear as "Operator = [<]" in code.
    [[<]] = 1
    [[<=]] = 2
    [[=]] = 3
    [[>=]] = 4
    [[>=]] = 5
End Enum

Public Function ComparisonIsTrue(ByVal sourceValue As Variant, ByVal operator As comparisonOperator, ByVal comparisonValue As Variant) As Boolean

    Dim isTrue As Boolean
    
    Select Case operator
    
        Case Is = 1 '/ <
        isTrue = (Value < comparisonValue)
        
        Case Is = 2 '/ <=
        isTrue = (Value <= comparisonValue)
        
        Case Is = 3 '/ =
        isTrue = (Value = comparisonValue)
        
        Case Is = 4 '/ >=
        isTrue = (Value >= comparisonValue)
        
        Case Is = 5 '/ >
        isTrue = (Value > comparisonValue)
        
        Case Else
        '/ Error Handling
        Stop
    
    End Select
    
    ComparisonIsTrue = isTrue

End Function