True Or False

Filed under Code Garage, VB Feng Shui

It seems like such a simple task; check a variable for whether it contains a true value or a false value.

No problem, right? Well, if you’re dealing with numbers, maybe, but when input comes from config files or databases, the truth <ahem> may not be so obviously out there.

As part of my code garage, I thought I’d post two functions I’ve used for ages to do just that; convert a variable, virtually any variable, to a boolean result.

They are especially useful in configuration handling, where you might want to support multiple values that might mean “true” or “false”, like on/off, yes/no, etc. I’ve also found that they can make code clearer by specifying the “assumed” default value if the variable can’t be concretely identified one way or another (is “Bob Thomas” true or false?).

Public Function IsTrue(VarToTest As Variant, Optional ByVal Default As Boolean = True) As Boolean
   '---- Resolve an input variable to a boolean
   '     but convert common "true"/"false" phrases as well
   '     Also, this allows for an easy way to indicate a
   '     "default" value in cases of an undetermined (ie blank)
   '     value
   Dim t$
   Dim s

   Select Case VarType(VarToTest)
      Case vbArray
         Err.Raise 5, "IsTrue", "Can't test an array for true"
      Case vbObject
         IsTrue = ObjPtr(VarToTest) <> 0
      Case vbString
         If Len(VarToTest) = 0 Then
            '---- is true assumes blanks are true
            IsTrue = Default
         Else
            On Error Resume Next
            '---- strip to first space
            t$ = Trim$(Left$(VarToTest, 15))
            s = InStr(t$, " ")
            If s > 0 Then t$ = Left$(t$, s - 1)
            '---- clean out any tabs
            t$ = Replace(t$, Chr$(9), vbNullString)
            '---- accept some synonyms (any other good ones?)
            If InStr(1, t$, "YES", vbTextCompare) = 1 Then
               IsTrue = True
            ElseIf InStr(1, t$, "NO", vbTextCompare) = 1 Then
               IsTrue = False
            ElseIf InStr(1, t$, "ON", vbTextCompare) = 1 Then
               IsTrue = True
            ElseIf InStr(1, t$, "OFF", vbTextCompare) = 1 Then
               IsTrue = False
            ElseIf InStr(1, t$, "TRUE", vbTextCompare) = 1 Then
               IsTrue = True
            ElseIf InStr(1, t$, "FALSE", vbTextCompare) = 1 Then
               IsTrue = False
            Else
               IsTrue = CBool(VarToTest)
               If Err Then
                  IsTrue = Val(VarToTest) <> 0
               End If
               On Error GoTo 0
            End If
         End If
      Case Else
         If IsEmpty(VarToTest) Then
            IsTrue = Default
         Else
            On Error Resume Next
            IsTrue = CBool(VarToTest)
            If Err Then
               IsTrue = Default
            End If
            On Error GoTo 0
         End If
   End Select
End Function


Public Function IsFalse(VarToTest As Variant, Optional ByVal Default As Boolean = False) As Boolean
   '---- basically, the inverse of IsTrue above
   '     mainly for convenience

   IsFalse = Not IsTrue(VarToTest, Default)
End Function

Hey, CBOOL is undoubtedly faster, but:

   If IsFalse(SettingValue) Then 
      '---- handle the negative condition here
   End If

just seems so much clearer.

The optional Default argument allow you to specify what value to return if the value to test can’t be resolved satisfactorily one way or the other. Practically, this allows you to easily specify whether a blank value or a “non-boolean” value should translate to true or false. This is especially important when you’re reading config options from a file or the registry where the option may not exist at all.

It allows to do something like so: 

   If IsTrue(SettingValue, False) Then 
      '---- handle positive conditions here, but if the SettingValue is blank, we default to False
   End If

Simple, but handy.

3 Comments

  1. Ralf says:

    I *still* think you can test an array for Truthiness.

    Interate through each element and keep track of its calculated True/False value. Calculate an average. For the whole array, >.49 = True.

    Kind of like the "crisping" procedure in fuzzy math.

  2. Darin says:

    Hmm, never heard of "crisping" in fuzzy math, but after a few googles, now I know what that means. Interesting.

    How often I could use it in my projects, I’m not sure…

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*