KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A simple boolean test and assignment wrapper function
PRODUCT: 4D | VERSION: 6.5 | PLATFORM: Mac & Win
Published On: June 15, 2001

Often, when writing methods, multiple If/Else/End if statements may be required. Wrapping a generic If/Else/End if statement into a function can reduce a (minimum) 5-line statement down to just a single line. The result is more compact code that can make a long method easier to read.

For example, these 6 lines of code...

` Project Method: DirectorySymbol --> Number
` $0 INTEGER ASCII of "/" (Windows) or ":" (MacOS)

C_INTEGER($0)

If (OnWindows )
$0:=Ascii("\")
Else
$0:=Ascii(":")
End if

...can now be reduced to just two line of code…

C_INTEGER($0)
$0:=BooleanLongint (OnWindows ;Ascii ("\");Ascii (":"))

…by using this method:

` Project Method: BooleanLongint (Boolean; number {;number }) --> Number
` $0LONGINITValue returned
` $1BOOLEANBoolean value to test
` $2LONGINITNumber to PASS if $1 = TRUE
` $3LONGINITNumber to PASS if $1 = FALSE {optional}
` otherwise an empty string is passed ("")

` PURPOSE OF ROUTINE: Analyze ($1) and if TRUE pass Number ($2),
` if FALSE: pass Number ($3) or $3 isn't available as a parameter,
` simply pass an "Empty" string.

C_LONGINT($0)
C_BOOLEAN($1)
C_LONGINT($2)
C_LONGINT($3)


` Code begins:

If ($1)
 $0:=$2
Else
If ((Count parameters )=3)
 $0:=$3
 Else
 $0:=0
 End if
End if