An intro to object-oriented programming in 4D: Classes

Automatically translated from English

Many of you have have been asking to be able to define an object type ever since the Object type became available. Thanks to object notation, many of you dream of having object functions.  Dream no more and say hello to classes in 4D v18 R3 project database! In this blog post, we’re introducing one of the most interesting concepts of object-oriented programming … along with a database example and a bonus video!

Example with “Person” class

Note: Starting with 4D v18 R3, all the HDIs will be hosted on GitHub. You can either clone the HDI or directly download the Zip file. Checkout this paragraph for details.

What’s a class?

A class is a user-defined data type that allows you to define the behavior of an object. It contains its own member functions that can be accessed and used when an instance of the class is created.

How to Create a class

You can create a class from the New menu in the toolbar or from the Explorer dialog. Now that the class editor is displayed, you can create the constructor and the functions.

How to create class functions?

To create a function, you must use the Function keyword followed by the name of the function. Then, you can pass parameters (as with methods).

Function methodName
  $0:="Hi "+$1

What is the class store?

You already know the ds command (which allows you to access the datastore). Now using the same logic, you have the cs command which returns the class store. All of the classes that you create are visible and available in the class store.

To create an object that is an instance of a class, you use the cs command and the new member function.


What is a constructor?

A constructor is used to create and instantiate an object with default attributes. It’s called automatically when the object is initialized.

In the editor, simply write Class constructor. You can pass parameters to your constructor to initialize certain variables, just like you can do with a method using $1, $2, etc.

For the class:

Class constructor

For the method which instantiates an object of the class, pass the parameter to the new member function:



Let’s take a look at a concrete example with a “Person” class. I create:

  • a constructor,
  • a getFullName function,
  • a getAge function.


Here’s the content of my class:

Class constructor
  C_TEXT($1) // FirstName
  C_TEXT($2) // LastName
  C_DATE($3) // Birthdate
Function getFullName
  $0:=This.firstName+" "+Uppercase(This.lastName)
Function getAge
  $0:=(Current date-This.birthdate)/365.25

Now, in a method, I create a “Person” object. Then, I read the attributes and execute the functions.

$"John";"Doe";!1956-10-27!) // instantiate the object
$firstname:=$o.firstName // return John
$lastname:=$o.lastName // return Doe
$fullName:=$o.getFullName() // return John DOE
$age:=$o.getAge() // return 63


What’s next?

In the next blog post, we’ll discuss the notion of inheritance between classes.

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.