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!
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
C_TEXT($0;$1)
$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.
C_OBJECT($o)
$o:=cs.myClass.new()
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
C_LONGINT($1)
This.variable:=$1
For the method which instantiates an object of the class, pass the parameter to the new member function:
C_OBJECT($o)
$o:=cs.Class1.new(8)
Example
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
This.firstName:=$1
This.lastName:=$2
This.birthdate:=$3
Function getFullName
C_TEXT($0)
$0:=This.firstName+" "+Uppercase(This.lastName)
Function getAge
C_LONGINT($0)
$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.
C_OBJECT($o)
$o:=cs.Person.new("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.