Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

For a while now I've been playing around with various ways of passing a logical predicate E.G. >= someValue as an argument to a function. (see: If ComparisonIsTrue(thisPost, ComparisonOperator.NotEqualTo, goodCode) Then Me.Answer and a global enum for comparison operators)

For a while now I've been playing around with various ways of passing a logical predicate E.G. >= someValue as an argument to a function. (see: If ComparisonIsTrue(thisPost, ComparisonOperator.NotEqualTo, goodCode) Then Me.Answer and a global enum for comparison operators)

Tweeted twitter.com/StackCodeReview/status/732577128722632705
deleted 2 characters in body
Source Link
Kaz
  • 8.8k
  • 2
  • 30
  • 68
Option Explicit

Private pOperator As ComparisonOperator
Private pRightValue As Variant

Private Const NULL_ERROR_TEXT As String = "Invalid Compare input. Cannot compare against Null"
Private Const OBJECT_ERROR_TEXT As String = "Invalid compare input. Input must be a value, not an object"

Public Property Let Operator(ByVal inputOperator As ComparisonOperator)
    pOperator = inputOperator
End Property

Public Property Let RightValue(ByVal inputValue As Variant)
    If IsNull(inputValue) Then
        PrintErrorMessage NULL_ERROR_TEXT
        Stop
    ElseIf IsObject(inputValue) Then
        PrintErrorMessage OBJECT_ERROR_TEXT
        Stop
    End If

    pRightValue = inputValue
End Property

Public Function Compare(ByVal leftValue As Variant) As Boolean

    If IsNull(inputValueleftValue) Then
        PrintErrorMessage NULL_ERROR_TEXT
        Stop
    ElseIf IsObject(inputValueleftValue) Then
        PrintErrorMessage OBJECT_ERROR_TEXT
        Stop
    End If

    Dim isTrue As Boolean
    Select Case pOperator
        Case NotEqualTo
            isTrue = (leftValue <> pRightValue)
    
        Case LessThan
            isTrue = (leftValue < pRightValue)
        
        Case LessThanOrEqualTo
            isTrue = (leftValue <= pRightValue)
        
        Case EqualTo
            isTrue = (leftValue = pRightValue)
        
        Case GreaterThanOrEqualTo
            isTrue = (leftValue >= pRightValue)
        
        Case GreaterThan
            isTrue = (leftValue > pRightValue)
        
        Case Else
            '/ TODO: Error Handling
            Stop
    End Select
        
    Compare = isTrue

End Function
Option Explicit

Private pOperator As ComparisonOperator
Private pRightValue As Variant

Private Const NULL_ERROR_TEXT As String = "Invalid Compare input. Cannot compare against Null"
Private Const OBJECT_ERROR_TEXT As String = "Invalid compare input. Input must be a value, not an object"

Public Property Let Operator(ByVal inputOperator As ComparisonOperator)
    pOperator = inputOperator
End Property

Public Property Let RightValue(ByVal inputValue As Variant)
    If IsNull(inputValue) Then
        PrintErrorMessage NULL_ERROR_TEXT
        Stop
    ElseIf IsObject(inputValue) Then
        PrintErrorMessage OBJECT_ERROR_TEXT
        Stop
    End If

    pRightValue = inputValue
End Property

Public Function Compare(ByVal leftValue As Variant) As Boolean

    If IsNull(inputValue) Then
        PrintErrorMessage NULL_ERROR_TEXT
        Stop
    ElseIf IsObject(inputValue) Then
        PrintErrorMessage OBJECT_ERROR_TEXT
        Stop
    End If

    Dim isTrue As Boolean
    Select Case pOperator
        Case NotEqualTo
            isTrue = (leftValue <> pRightValue)
    
        Case LessThan
            isTrue = (leftValue < pRightValue)
        
        Case LessThanOrEqualTo
            isTrue = (leftValue <= pRightValue)
        
        Case EqualTo
            isTrue = (leftValue = pRightValue)
        
        Case GreaterThanOrEqualTo
            isTrue = (leftValue >= pRightValue)
        
        Case GreaterThan
            isTrue = (leftValue > pRightValue)
        
        Case Else
            '/ TODO: Error Handling
            Stop
    End Select
        
    Compare = isTrue

End Function
Option Explicit

Private pOperator As ComparisonOperator
Private pRightValue As Variant

Private Const NULL_ERROR_TEXT As String = "Invalid Compare input. Cannot compare against Null"
Private Const OBJECT_ERROR_TEXT As String = "Invalid compare input. Input must be a value, not an object"

Public Property Let Operator(ByVal inputOperator As ComparisonOperator)
    pOperator = inputOperator
End Property

Public Property Let RightValue(ByVal inputValue As Variant)
    If IsNull(inputValue) Then
        PrintErrorMessage NULL_ERROR_TEXT
        Stop
    ElseIf IsObject(inputValue) Then
        PrintErrorMessage OBJECT_ERROR_TEXT
        Stop
    End If

    pRightValue = inputValue
End Property

Public Function Compare(ByVal leftValue As Variant) As Boolean

    If IsNull(leftValue) Then
        PrintErrorMessage NULL_ERROR_TEXT
        Stop
    ElseIf IsObject(leftValue) Then
        PrintErrorMessage OBJECT_ERROR_TEXT
        Stop
    End If

    Dim isTrue As Boolean
    Select Case pOperator
        Case NotEqualTo
            isTrue = (leftValue <> pRightValue)
    
        Case LessThan
            isTrue = (leftValue < pRightValue)
        
        Case LessThanOrEqualTo
            isTrue = (leftValue <= pRightValue)
        
        Case EqualTo
            isTrue = (leftValue = pRightValue)
        
        Case GreaterThanOrEqualTo
            isTrue = (leftValue >= pRightValue)
        
        Case GreaterThan
            isTrue = (leftValue > pRightValue)
        
        Case Else
            '/ TODO: Error Handling
            Stop
    End Select
        
    Compare = isTrue

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

Enum:

Public Enum ComparisonOperator
    NotEqualTo = 0
    LessThan = 1
    LessThanOrEqualTo = 2
    EqualTo = 3
    GreaterThanOrEqualTo = 4
    GreaterThan = 5
End Enum

Code:

Option Explicit

Private pOperator As ComparisonOperator
Private pRightValue As Variant

Private Const NULL_ERROR_TEXT As String = "Invalid Compare input. Cannot compare against Null"
Private Const OBJECT_ERROR_TEXT As String = "Invalid compare input. Input must be a value, not an object"

Public Property Let Operator(ByVal inputOperator As ComparisonOperator)
    pOperator = inputOperator
End Property

Public Property Let RightValue(ByVal inputValue As Variant)
    If IsNull(inputValue) Then
        PrintErrorMessage "Invalid Compare input. Cannot compare against Null"NULL_ERROR_TEXT
        Stop
    End If
    IfElseIf IsObject(inputValue) Then
        PrintErrorMessage "Invalid compare input. Input must be a value, not an object"OBJECT_ERROR_TEXT
        Stop
    End If

    pRightValue = inputValue
End Property

Public Function Compare(ByVal leftValue As Variant) As Boolean

    If IsNull(leftValueinputValue) Then
        PrintErrorMessage "Invalid Compare input. Cannot compare Null"NULL_ERROR_TEXT
        Stop
    End If
    
    IfElseIf IsObject(leftValueinputValue) Then
        PrintErrorMessage "Invalid compare input. Input must be a value, not an object"OBJECT_ERROR_TEXT
        Stop
    End If

    Dim isTrue As Boolean
    Select Case pOperator
        Case NotEqualTo
            isTrue = (leftValue <> pRightValue)
    
        Case LessThan
            isTrue = (leftValue < pRightValue)
        
        Case LessThanOrEqualTo
            isTrue = (leftValue <= pRightValue)
        
        Case EqualTo
            isTrue = (leftValue = pRightValue)
        
        Case GreaterThanOrEqualTo
            isTrue = (leftValue >= pRightValue)
        
        Case GreaterThan
            isTrue = (leftValue > pRightValue)
        
        Case Else
            '/ TODO: Error Handling
            Stop
    End Select
        
    Compare = isTrue

End Function

Code:

Option Explicit

Private pOperator As ComparisonOperator
Private pRightValue As Variant

Public Property Let Operator(ByVal inputOperator As ComparisonOperator)
    pOperator = inputOperator
End Property

Public Property Let RightValue(ByVal inputValue As Variant)
    If IsNull(inputValue) Then
        PrintErrorMessage "Invalid Compare input. Cannot compare against Null"
        Stop
    End If
    If IsObject(inputValue) Then
        PrintErrorMessage "Invalid compare input. Input must be a value, not an object"
        Stop
    End If

    pRightValue = inputValue
End Property

Public Function Compare(ByVal leftValue As Variant) As Boolean

    If IsNull(leftValue) Then
        PrintErrorMessage "Invalid Compare input. Cannot compare Null"
        Stop
    End If
    
    If IsObject(leftValue) Then
        PrintErrorMessage "Invalid compare input. Input must be a value, not an object"
        Stop
    End If

    Dim isTrue As Boolean
    Select Case pOperator
        Case NotEqualTo
            isTrue = (leftValue <> pRightValue)
    
        Case LessThan
            isTrue = (leftValue < pRightValue)
        
        Case LessThanOrEqualTo
            isTrue = (leftValue <= pRightValue)
        
        Case EqualTo
            isTrue = (leftValue = pRightValue)
        
        Case GreaterThanOrEqualTo
            isTrue = (leftValue >= pRightValue)
        
        Case GreaterThan
            isTrue = (leftValue > pRightValue)
        
        Case Else
            '/ TODO: Error Handling
            Stop
    End Select
        
    Compare = isTrue

End Function

Enum:

Public Enum ComparisonOperator
    NotEqualTo = 0
    LessThan = 1
    LessThanOrEqualTo = 2
    EqualTo = 3
    GreaterThanOrEqualTo = 4
    GreaterThan = 5
End Enum

Code:

Option Explicit

Private pOperator As ComparisonOperator
Private pRightValue As Variant

Private Const NULL_ERROR_TEXT As String = "Invalid Compare input. Cannot compare against Null"
Private Const OBJECT_ERROR_TEXT As String = "Invalid compare input. Input must be a value, not an object"

Public Property Let Operator(ByVal inputOperator As ComparisonOperator)
    pOperator = inputOperator
End Property

Public Property Let RightValue(ByVal inputValue As Variant)
    If IsNull(inputValue) Then
        PrintErrorMessage NULL_ERROR_TEXT
        Stop
    ElseIf IsObject(inputValue) Then
        PrintErrorMessage OBJECT_ERROR_TEXT
        Stop
    End If

    pRightValue = inputValue
End Property

Public Function Compare(ByVal leftValue As Variant) As Boolean

    If IsNull(inputValue) Then
        PrintErrorMessage NULL_ERROR_TEXT
        Stop
    ElseIf IsObject(inputValue) Then
        PrintErrorMessage OBJECT_ERROR_TEXT
        Stop
    End If

    Dim isTrue As Boolean
    Select Case pOperator
        Case NotEqualTo
            isTrue = (leftValue <> pRightValue)
    
        Case LessThan
            isTrue = (leftValue < pRightValue)
        
        Case LessThanOrEqualTo
            isTrue = (leftValue <= pRightValue)
        
        Case EqualTo
            isTrue = (leftValue = pRightValue)
        
        Case GreaterThanOrEqualTo
            isTrue = (leftValue >= pRightValue)
        
        Case GreaterThan
            isTrue = (leftValue > pRightValue)
        
        Case Else
            '/ TODO: Error Handling
            Stop
    End Select
        
    Compare = isTrue

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