Formula – Think outside the box

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";\

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.


Then, use generic code in the buttons:
// Or

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
      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! 

Vanessa Talbot
• Product Owner •Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.