KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using Class Functions
PRODUCT: 4D | VERSION: 18 R3 | PLATFORM: Mac & Win
Published On: September 15, 2020

4D v18 R3 introduces a new Classes feature that allows us to create methods (called functions) that are shared between object instances of the same class. This is great for writing more modular, cleaner code. For example, let's create a new "Car" class:



Note that the Classes feature is only available in a project database. After our Car class is created, we can write functions for the car class. Let's write a turnLeft function:


The function will simply alert that the car has turned left. To use the function, we need to create a new instance of a Car:



We do this by calling the new class store command cs, accessing the Car class that we just created, and invoking the new() method. Now, we can "test drive" the car by invoking the turnLeft function for our car:



We can do this for other objects of the same class as well: