4D Blog

Home Tips Formula: More power behind simplicity

Formula: More power behind simplicity

March 26, 2019

Tips

The Formula command is available since 4D v17 R3. So far you might have thought of assigning a formula as a method to an object or building smart objects that can calculate values or do anything you need. That’s really great, and there’s more than meets the eye!

I had a great demonstration from Vincent de Lachaux – Developer and expert 4D – on how he uses this command. For this blog post, I compiled different scenarios from that demo to give you insight on a different dimension of this command.

Example 1 – To simplify an existing command

Some 4D commands are used hundreds of thousands of times in the code. We often pass the same parameters except one.

Let’s take the simple example of the OBJECT get pointer command. I want to initialize the values of my check boxes, so I write:

(OBJECT get pointer(Object named;"checkbox"))->:=0

Or, I write a formula that takes the name of my object as a parameter:

interface.pointer:=Formula(OBJECT Get pointer(Object named;$1))

When I want to assign or retrieve the value of my check box, I simply write:

(interface.pointer("checkbox"))->:=0
(interface.pointer("checkbox2"))->:=0

Or, if I need to set thousands of check boxes, I can create a formula to make things simple:

interface.setCheckbox:=Formula((OBJECT Get pointer(Object named;$1))->:=$2)

To set a series of check boxes, I can simply write:

interface.setCheckbox("checkbox1";0)
interface.setCheckbox("checkbox2";1)
interface.setCheckbox("checkbox3";0)
interface.setCheckbox("checkbox4";1)

Easier to read – and much easier to write, as I don’t need to count parenthesis…

Example 2 – With a database parameter

In your code, set the same database parameter several times with different values. Let’s take a concrete example with delay control tips. You may want to display the tips without delay in some parts of your forms, and to wait 1s in other parts. Each time you use the database parameter, you must remember the value or look for it in another piece of code.

Why not creating a formula?

interface.tipsNoDelay:=Formula(SET DATABASE PARAMETER(Tips delay;1))
interface.tips1sDelay:=Formula(SET DATABASE PARAMETER(Tips delay;60))

Then, when you want to change the value, you just call the formula:

interface.tips1sDelay()
// or
interface.tipsNoDelay()

Example 3 – Use formula as a variant

You can create a formula that uses the values passed as parameters of a method (i.e. $1, $2) but, in the method in which you create your formula, you do not need to type $1 and $2. The variables are typed when the formula is executed. You can write generic methods or formulas.

Here is a simple example showing that the type is defined at execution. 

$obj:=New object("addition";Formula($1+$2))

$number:=$obj.addition(1;2) // returns 3
$text:=$obj.addition("hello ";"world") // returns hello world

Example 4 –  Test a series of conditions

If you make a sequence of conditions, such as If (() & ()), 4D always evaluates all conditions. It does not stop and keeps evaluating the following ones. If some conditions can only be evaluated if the leftmost are true, you are forced to make a deep nested list of if conditions, to check if the object is valid before testing the attribute. This is how it goes:

If (Not(Undefined($o))
   If ($o.value#Null)
      If ($o.value<5)
         // to do
      End if
   End if
End if

In this example, we had “only” 3 conditions, but you can easily notice that it becomes more and more complex. What about writing everything in a single condition? Why not writing a method that executes your formulas one after the other? If the result is True, you evaluate the following formula, if the result is false, you stop and return False.

If (_AND (\
   Formula(Not(Undefined($o)));\
   Formula($o.value#Null);\
   Formula($o.value<5)\
))
   // to do
End if

For the _AND method, very usual and simple code:

$0:=True
For ($i;1;Count parameters;1)
   $0:=${$i}.call()
   If (Not($0))
      $i:=MAXLONG-1 // Stop if the condition fails
   End if
End for

I hope these examples will give you insight on other ways to use this command. Feel free to share your formulas on the 4D forum… I’m sure you have plenty of ideas!

 

Discuss

Tags Formula, Programming, v17 R3

Latest related posts

  • April 28, 2025

    Tips & Tricks for your 4D Apps – April 2025 Edition

  • April 9, 2025

    Restrict data according to privileges or information saved in session storage

  • April 7, 2025

    Designing an Interactive and Dynamic Menu Bar in Qodly application

Vanessa Talbot
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.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy