KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Consider Your Variable's Role When Choosing Its Scope
PRODUCT: 4D | VERSION: 11.5 | PLATFORM: Mac & Win
Published On: February 26, 2010

It is important when developing or refactoring the code in a 4D database to give consideration to how a variable is used before deciding whether to use a local, process, or inter-process variable.

Take for example the 4D commands INSERT LISTBOX COLUMN and INSERT LISTBOX COLUMN FORMULA. In both commands the last parameter calls for an "Integer variable". The documentation does not call for any specific value to be placed in the variable, just to: "Pass the object name and the variable of the inserted column header in the headerName and headerVar parameters."

C_LONGINT(Hdr_1;$last)
$last:=Get number of listbox columns(*;"LBox1")+1
INSERT LISTBOX COLUMN(*;"LBox1";$last;"FieldCol";[myTable]myFld;"HeaderName";Hdr_1)


In these commands the "headerVar" (Hdr_1) is used by 4D for data storage, so the variable must always be "in scope" for the List Box to work correctly and reliably. For the variable to be "in scope" in a compiled database the variable must be either a process or inter-process variable. If, instead, you use a local variable, the code will compile without error but has the potential to cause problems when the user clicks on the header to sort the list as 4D will attempt to store a value in an inappropriate address.