Tech Tip: How to write a Getter/Setter method
PRODUCT: 4D | VERSION: 12.1 | PLATFORM: Mac & Win
Published On: March 14, 2011
A nice technique that borrows from the OOP world is the ability to write a 4D Project method that is both a Getter and a Setter function.
The benefits and advantages of this technique are:
- It is self-documenting and keeps the variable from being directly touched and manipulated by other methods.
- Without knowing the name or purpose of the variable it's value can be set.
- Any business rules regarding the variable, such as how many decimal places, or dates range, or string length can be enforced in one place.
- Getter/Setter methods (GSM) vs. Interprocess variables
- Interprocess variable can be accessed and changed by any user.
- GSM can be restricted to specific user groups in the method properties.
An example Getter/Setter function is below
If (True) If (False) //***************************************************************************** // // MyGetAndSetFunction({ $MyOptionalVar }) -> Response // // $0 - type - Returned value of process or interprocess var // $1 - type - New value to be stored in process or interprocess var // //***************************************************************************** End if C_TEXT($MethodName_T) $MethodName_T:=Current method name //===================== Declare Variables ================================== //method_parameters_declarations C_REAL( $0; $1 ) //---------------------------------------------------------------------- //method_wide_constants_declarations C_REAL( MyShipAndHandlingRate_R ) //---------------------------------------------------------------------- //local_variable_declarations C_LONGINT($Params_L) End if //====================== Initialize and Setup ================================ $Params_L:=Count parameters //======================== Method Actions ================================== If ($Params_L=1) MyShipAndHandlingRate_R:=Round( $1; 4 ) End if $0:=MyShipAndHandlingRate_R |
Commented by Wayne Stewart on April 10, 2011 at 7:52 PM
I use this technique all the time and have created a template to assist with creating the method. Another advantage is that 4D will be able to use type ahead in the method editor.