KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Generic coding technique: Using RESOLVE POINTER to check for nil pointers
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: August 28, 2002

Compatibility: 6.7.x and 6.8.x

The ability to check for nil pointers can be a critical technique for writing generic code. This tech tip discusses such an implementation.

I was working on an application that required that the records displayed in some, but not all, of the output forms be sorted. Since the output forms required the same coding, the code was placed in the project method Shell_OutputFormMethod. This method was called from the form method of each output form as explained below:

If records were to be sorted, a pointer to a column was passed:
Shell_OutputFormMethod(->[Contact]LastName)

If records were NOT to be sorted, a pointer to a column was NOT passed:
Shell_OutputFormMethod()

Note: The parenthesis must be used even if you are not passing the column pointer parameter, otherwise $1 will be undefined in Shell_OutputFormMethod causing an error.



As mentioned, the code for the output form was placed in the project method Shell_OutputFormMethod:

C_POINTER($pDefaultSortField;$1)
C_INTEGER($LFormEvent)

$pDefaultSortField:=$1
$LFormEvent:=Form event

Case of
: (Form event=On Load )
Shell_OrderRecordsByColumn ()

: ($LFormEvent=On Close Detail )
WIN_OutputWindowTitle
MENU BAR(3)
End case



The code of the subroutine Shell_ OrderRecordsByColumn was as follows:

C_POINTER($pDefaultSortField;$1)
C_TEXT($tVarName)
C_INTEGER($itableNum;$ifieldNum)

$pDefaultSortField:=$1

RESOLVE POINTER($pDefaultSortField;$tVarName;$itableNum;$ifieldNum)
If (Not($tVarName=""))
ORDER BY(Shell_PointerToTableForField ($pDefaultSortField)->;$pDefaultSortField->;>)
End if

Here is how this works:
RESOLVE POINTER returns a null string in the text variable $tVarName if no pointer has been passed to Shell_ OrderRecordsByColumn. We can then check for this null string and prevent the ORDER BY command from executing. This technique allows the generic Shell_OutputFormMethod to be used whether or not a column needs to be sorted for a specific output form.