KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Class constructor special function
PRODUCT: 4D | VERSION: 18 R3 | PLATFORM: Mac & Win
Published On: September 1, 2020

4D v18 R3 now allows us to create Classes with different functions for cleaner, more flexible code. The Class constructor is a special type of function that can be used by any Class. It is automatically called every time a new class object is initialized, as long as the class constructor exists. It allows us to pass arguments into an initialized class object, usually to map values to certain properties of the object. For example, let's say we have a Car class with a constructor like below:



Inside the constructor we declare two text parameters. Using This, we can say that the first parameter will be the make of the car, and the second one will be its model. Then, we can initialize a new Car object and choose its make and model like below:



We simply call cs.Car.new() and pass in our desired make and model. Note that the order that we pass in the values matters, like in any other method. Inspecting in the debugger, we can see that the values have indeed been mapped to the car object's properties: