# The Use of Formulas in Collections & Callback Commands

When you use collections with a member method that needs a callback method, many of you have asked us for a more straightforward way.

Starting with 4D v19 R6, 4D allows you to use a formula to define a callback in the collection member functions, the EXECUTE METHOD IN SUBFORM, CALL FORM, and CALL WORKER commands. If you can reduce your code to a simple expression, you can pass it directly into the formula without using a method.

HDI formula in commands

## formula in COLLECTIoN

You can now use formula as callback in the members methods:

For example, if you want to filter the collection to find all the members greater than 0, you can do it on one line:

``\$result:=\$c.filter(Formula(\$1.value>0))``

As you can see above, to be more simple and readable, we make the \$1.result optional in the formulas.

Of course, you can always use the code:

``````\$c:=New collection
\$c.push(1;-5; -3; -1;3; -4; -6; -2;10)
\$result:=\$c.filter("NumberGreaterThan0")``````

but you can use formula too to refer to a method and so benefit from the code completion to find your method name, and take advantage of automatic renaming if you change the name of your method:

``````\$c:=New collection
\$c.push(1;-5; -3; -1;3; -4; -6; -2;10)
\$result:=\$c.filter(Formula(NumberGreaterThan0))
``````

with the NumberGreaterThan0 method:

``````#DECLARE (\$param:Object)
\$param.result:=\$param.value>0``````

## formula in commands

You can use a formula to define a callback in the EXECUTE METHOD IN SUBFORM, CALL FORM, and CALL WORKER commands.

As for the collections, you can use expressions without having to create a method:

``````// Close the windows in the context of the form
CALL FORM(\$windows;formula(CANCEL))``````

or refer to a method with one of these 2 ways:

``EXECUTE METHOD IN SUBFORM("Subform"; Formula(UpdateField))``

or

``EXECUTE METHOD IN SUBFORM("Subform"; "UpdateField")``

Try all these new features with the HDI above, and do not hesitate to give your feedback on the forum!

• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.