Tech Tip: Comparisons with Null
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: July 29, 2021
Since Null is not a member of any data type, it is not considered a "value", but rather more of a placeholder to indicate an undefined value. When doing comparisons in with a variable that could be Null, make sure to include an additional check to see if the variable is Null before the comparison.
In the example below, the "If" statement checks to see if $ob.b is equal to "b". This will fail because $ob.b is Null and Null cannot be compared with a string. Similarly to how different data types cannot be compared.
C_OBJECT($ob) $ob:=New object $ob.a:="a" If ($ob.b="b") //$ob.b = "b" Else //$ob.b # "b" End if |
The statment rewritten with the extra check for Null may look like this. Similar to "If" statments, other comparisons such as .query() all require an additional check for Null if the variable has the possiblity of being Null.
C_OBJECT($ob) $ob:=New object $ob.a:="a" If ($ob.b#Null) If ($ob.b="b") //$ob.b = "b" Else //$ob.b # "b" End if Else //$ob.b = Null End if |