According to the top 10 blog posts of 2019, the Formula: More power behind simplicity post ranked quite high … in the top five. It seems that Formula really grabbed your attention, so here’s another tip that Vincent de Lachaux (developer and 4D expert) has shown me and I’m sharing with you!
Use formula in a generic dialog
The FORM command lets you easily create a generic alert dialog. Simply direct the dialog to different texts to be displayed in an object:
$object:=New Object(\
"mainText";"Are you sure you want to empty the trash?";\
"additionalText";"You can't undo this action.";\
"okText";"Empty Trash";\
"cancelText";"Cancel"\
)
You can further enrich this object with a formula for the actions behind the OK and Cancel buttons. Your formula can call a 4D command or project method, with or without parameters.
$object.okAction:=Formula(myMethod("Trash";"ok"))
$object.cancelAction:=Formula(myMethod("Trash";"cancel"))
Then, use generic code in the buttons:
Form.cancelAction.call()
// Or
Form.okAction.call()
That’s it! You now have a completely generic alert dialog.
Add formulas in the Storage object
If you have verification or calculation functions anywhere in your code, you can add formulas to the Storage object.
Here’s an example:
// Define common functions
If (Storage.ƒ=Null)
Use (Storage)
Storage.ƒ:=New shared object
Use (Storage.ƒ)
// Register the function in Storage
Storage.ƒ.isValid:=Formula(....)
Storage.ƒ.computeX:=Formula(....)
End use
End use
End if
Using this function, you don’t change the content of the storage, so you don’t need Use/End Use.
Create formulas when loading a component
To use the methods of a component in the host database, you must first share the method. However, when you’re adding methods, it can be easy to forget. Here’s an idea for sharing component formulas!
In your component, create a method that returns an object to the host database. This object contains formulas that call the methods of your component. Result? Only one method to share. Cool isn’t it!?
That’s all for this blog post. Now it’s your turn to share this tip with someone else!