4D Blog

Home Product Write your own methods for objects

Write your own methods for objects

October 17, 2018

Product

A new command is at your disposal allowing you to use formulas in your code. You can now encapsulate them in objects and call them when needed. There’s no more need to write your code as text. Now you can just pass your formula in your command and that’s it! This is a great addition, since your code isn’t text, you can benefit from Syntax highlighting and all the other advanced functionalities of the code editor!

Encapsulate a formula in an object

With the Formula command, it’s very simple to encapsulate a formula in an object:

$f:=Formula(ALERT("Hello world"))

And of course, because the formula is encapsulated in an object, you can encapsulate it in a property of an object and create your own encapsulated object methods:

$f:=New object
$f.message:=Formula(ALERT("Hello world"))
$f.message() // Display alert dialog box with the message "Hello world"

If you want to pass parameters to your formula, just use $1, $2 … as in your methods:

$f:=New object
$f.message:=Formula(ALERT("Hello "+$1))
$f.message("John")
// Display alert dialog box with the message "Hello John"

Or if you want to use the value of an object attribute, the This command is the answer:

$f:=New object
$f.text:="Hello World"
$f.message:=Formula(ALERT(This.text))
$f.message()
// Display alert dialog box with the message "Hello world"

Finally, you can use a method as a formula:

$person:=New object
$person.firstName:="John"
$person.lastName:="Smith"
$person.greeting:=Formula(Greeting )
$g:=$person.greeting("hello")
// $g:="hello John Smith"
$g:=$person.greeting("hi")
// $g:="hi John Smith"

With the Greeting method:

$0:=$1+" "+This.firstName+" "+This.lastName

You can find other examples of how to use Formula in this HDI:

HDI_NewFormula

Calling your formula

You can also execute a formula object without setting it as a property of another object by using the call() and apply() methods.

$f:=Formula(ALERT(String($1+" "+$2))
$f.call(null;"Hello";"world")
$f.apply(null;New collection("Hello";"world") //formula.apply object methods

Encapsulate a formula in an object from a string

There’s a way to use a text version of a formula to create a method with the Formula from string command:

$textFormula:=Request("Please type a formula")
 If(ok=1)
    $f:=Formula from string($textFormula)
    ALERT("Result = "+String($f.call()))
 End if

Discuss

Tags Formula, Objects, Programming, v17 R3, v18

Latest related posts

  • June 16, 2025

    Generate, share and use web sessions One-Time Passcodes (OTP)

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

Fabrice Mainguené
Fabrice Mainguené
• 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.
  • 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