Concept

# Constructor

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

A special method that is called automatically when an object is first created (instantiated).

## Usage

``` xojo
Constructor Parameter List
```

| Part           | Type | Description                  |
|----------------|------|------------------------------|
| Parameter List | Any  | Optional list of parameters. |

## Notes

When you create a new object, you will sometimes want to perform some sort of initialization on the object. The <span class="title-ref">Constructor</span> is a mechanism for doing this. A class's <span class="title-ref">Constructor</span> is the method that will be executed automatically when an instance of the class is created.

You write a <span class="title-ref">Constructor</span> for a custom class by creating a new method for the class and naming it “<span class="title-ref">Constructor</span>”. 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 <span class="title-ref">Constructor</span> for any subclass, the Code Editor automatically inserts code that calls the <span class="title-ref">Constructor</span> for its super class using the `Super</api/language/super>` keyword. If there is more than one <span class="title-ref">Constructor</span>, it inserts calls to all of them. This is because the subclass's <span class="title-ref">Constructor</span> overrides its super class's <span class="title-ref">Constructor</span> but the new subclass may not initialize itself correctly without a call to the super class's <span class="title-ref">Constructor</span>. 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 <span class="title-ref">Constructor</span>.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### 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 <span class="title-ref">Constructor</span> with no parameters to the base class and setting its scope to Private. Subclasses of the base class get their own public <span class="title-ref">Constructor</span>, but you will be unable to instantiate the base class.

You can also set a <span class="title-ref">Constructor</span> to Protected in order to force the subclass to implement/override the <span class="title-ref">Constructor</span> in order for it to be instantiated.

To summarize the differences:

> - Protected: The subclass must override the <span class="title-ref">Constructor</span> in order to be instantiated
> - Private: The base class cannot be instantiated; subclasses get a standard public <span class="title-ref">Constructor</span> and can be instantiated

## Sample code

This code creates a `DateTime</api/data_types/datetime>` object and sets it to 15 April, 2018.

``` xojo
Var d As New DateTime(2018, 4, 15)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code is from the example project *Custom Drag* in the Examples folder. The main window consists of a `DesktopLabel</api/user_interface/desktop/desktoplabel>` control and a `DesktopTextArea</api/user_interface/desktop/desktoptextarea>`. The user can drag the text in the `DesktopLabel</api/user_interface/desktop/desktoplabel>` into the `DesktopTextArea</api/user_interface/desktop/desktoptextarea>`. This is not possible by default but the ability to drag the text is enabled by the code in the MouseDown event of the `DesktopLabel</api/user_interface/desktop/desktoplabel>`. It is:

``` xojo
Var d As DragItem
d = New DragItem(Self, Me.Left, Me.Top, Me.Width, Me.Height)
d.Text = Me.Value
d.Drag
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code creates a new `FolderItem</api/files/folderitem>`. You can create a copy of a `FolderItem</api/files/folderitem>` by passing the `FolderItem</api/files/folderitem>` to be copied to the <span class="title-ref">Constructor</span>. The result is a copy of the passed `FolderItem</api/files/folderitem>` rather than a reference to it.

``` xojo
Var f, f2 As FolderItem
f = SpecialFolder.Desktop.Child("MyDocument")
f2 = New FolderItem(f)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code creates a new `Picture</api/graphics/picture>` instance that has an alpha channel (i.e., a transparency parameter).

``` xojo
Var width  As Integer = 2000 
Var height As Integer = 2000

' creates new picture
Var pic As New Picture(width, height)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This <span class="title-ref">Constructor</span> creates a `Picture</api/graphics/picture>` object that will mirror the content that is drawn into a `Canvas</api/user_interface/desktop/desktopcanvas>` in its Paint event.

``` xojo
p = New Picture(Canvas1.Width, Canvas1.Height)
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Super</api/language/super>` keyword; `Destructor</api/language/destructor>`, `Constructors and Destructors</getting_started/object-oriented_programming/constructors_and_destructors>` topics

</div>
