From Xojo Documentation
Fires automatically when an object is instantiated.
Constructor [parameter list]
|Parameter List||Any||Optional list of parameters.|
When you create a new object, you will sometimes want to perform some sort of initialization on the object. The constructor is a mechanism for doing this. A class’s constructor is the method that will be executed automatically when an instance of the class is created.
You write a constructor for a custom class by creating a new method for the class and naming it “Constructor”. The drop-down list for the Method name field suggests this name and the names of all other methods that can be overridden.
When you create a constructor for any subclass, the Code Editor automatically inserts code that calls the constructor for its super class using the Super keyword. If there is more than one constructor, it inserts calls to all of them. This is because the subclass’s constructor overrides its super class’s constructor but the new subclass may not initialize itself correctly without a call to the super class’s constructor. You can edit the inserted calls in the event that this assumption is incorrect.
Classes that have constructors have a section that uses the syntax for each constructor.
See the chapter on custom classes in the User Guide for more information on writing constructors.
Private and Protected Constructors
Sometimes it is helpful to create a class that cannot be instantiated so that it can be used as a base for other classes. This is called an "abstract" class. You can do this in Xojo by adding a single Constructor with no parameters to the base class and setting its scope to Private. Subclasses of the base class get their own public constructor, but you will be unable to instantiate the base class.
You can also set a Constructor to Protected in order to force the subclass to implement/override the constructor in order for it to be instantiated.
To summarize the differences:
- Protected: The subclass must override the constructor in order to be instantiated
- Private: The base class cannot be instantiated; subclasses get a standard public constructor and can be instantiated
This example creates a Date object and sets it to 15 April, 2018.
The following code is from the example project "Custom Drag" in the Examples folder. The main window consists of a Label control and a TextArea. The user can drag the text in the Label into the TextArea. This is not possible by default but the ability to drag the text is enabled by the code in the MouseDown event of the Label. It is:
The following example creates a new FolderItem. You can create a copy of a FolderItem by passing the FolderItem to be copied to the constructor. The result is a copy of the passed FolderItem rather than a reference to it.
The following example creates a new Picture <ShowIf version=2011r4>instance that has an alpha channel (i.e., a transparency parameter).
Dim height As Integer = 2000
// creates new picture
Dim pic As New Picture(width, height)