UserGuide

Constructors and Destructors

From Xojo Documentation

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. An object’s constructor is the method that is executed automatically when an instance of the class is created. Conversely, when an object is removed from memory, its destructor is called.

Constructors

You add a constructor to a class by adding a method to the class called Constructor. For convenience, “Constructor” is a choice in the Method Name popup menu in the Code Editor. A default constructor is implied for all classes. This constructor will be called automatically when an instance is created via the New operator.

You can override the default constructor or add your own constructors with parameters. If your constructor accepts parameters, you must pass the required number of parameters and they must be of the correct data type.

Whenever you create a constructor for a subclass, the Code Editor automatically adds a call to the super class’s constructor for you in the Code Editor and adds comments that explains its purpose.

// Calling the overridden superclass constructor.
Super.Constructor

This is added because the constructor that you are writing overrides its Super class’s constructor, but the new object may not be initialized correctly unless its Super class’s constructor executes. The Super method is how you call a method of a Super class that is being overridden by a method of the subclass.

Here is a simple example of a constructor. Suppose you have a class that contains customer information, with properties for FirstName and LastName. You might assign these values in this manner:

Var c As New Customer
c.FirstName = "Betty"
c.LastName = "Draper"

But you can instead add a Constructor to the class to initialize it with those values. This constructor takes firstName and lastName as parameters and assigns them to the corresponding properties:

Sub Constructor(fName As String, lName As String)
FirstName = fName
LastName = lName
End Sub

You can now initialize the customer like this:

Var c As New Customer("Betty", "Draper")

Initializing Control Subclasses

If you need to initialize an instance of a control subclass, don’t use the constructor. Instead, put the code that does the initialization in the Open event handler for the control.

When you access Inspector properties in the Constructor, they will not have the default value specified in the Inspector and will instead have the default value for the data type. If you assign values to these public properties, the values will get overridden with what is specified in the Inspector.

Constructor Scope

Like with methods, you can set a Constructor's scope to Public, Protected and Private. One useful technique is to disable the standard constructor so that you force your own constructor to be used. For example, with the Customer class described above, you could add an empty, Constructor with its scope set to private to prevent the standard "New Customer" from working. Instead, only the Constructor with the parameters is allowed. If your tried to write this code:

Var c As New Customer

You would get this compilation error:

The constructor of this class is protected, and can only be called from within this class

Destructors

You can also create a destructor, although they are not usually necessary. The destructor is called automatically when an instance of the parent class is deleted or goes out of scope — for example, when the user closes the window. Name the new method “Destructor”. Destructors take no parameters and do not return a value.

Destructors are called when the last reference to an object is removed, even if execution is in a destructor for another object. Note that this means you can cause a stack overflow if your destructor triggers other destructors in a deep recursion. However, such overflow will not happen as long as properties of the object are being cleaned up automatically. So, it is generally preferable to not set properties to Nil in your destructor, but instead allow them to be automatically cleaned up for you.

Memory Management

Refer to the Memory Management topic for information on how Xojo uses Automatic Reference Counting to manage memory used by objects.

See Also

UserGuide:Object-Oriented Programming topic