Operator

# New

<div class="rst-class">

forsearch

</div>

Operator

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

## Description

Used to create <span class="title-ref">New</span> instances of objects at runtime.

## Usage

``` xojo
objectReference = New ClassName [ (param1, param2,...) ]

Var objectReference As New ClassName [ (param1, param2,...) ]
```

| Part            | Type                        | Description                                                                                  |
|-----------------|-----------------------------|----------------------------------------------------------------------------------------------|
| objectReference | Must be a type of ClassName | Required. Variable/Property of type *ClassName*, its superclasses or implemented interfaces. |
| ClassName       | A class name                | Required. Any available class with a non-private constructor.                                |

## Notes

The `constructor</api/language/constructor>` for a class is called whenever you create a <span class="title-ref">New</span> instance of it with the <span class="title-ref">New</span> statement.

The <span class="title-ref">New</span> operator can appear as a modifier on a `Var</api/language/var>` statement that creates a <span class="title-ref">New</span> object. This usage takes the place of two lines of code: The `Var</api/language/var>` statement that creates the object and a separate <span class="title-ref">New</span> statement that instantiates the object.

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

### Creating <span class="title-ref">new</span> instances of all other types of objects

The <span class="title-ref">New</span> operator can be used to create a <span class="title-ref">New</span> instance of any type of class. The object reference must be defined as the class type, one of its superclasses or an interface that it implements.

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

### Constructors

When you create a <span class="title-ref">New</span> object, you will sometimes want to perform some sort of initialization on the object. The `constructor</api/language/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 <span class="title-ref">New</span> 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. There can be multiple overloaded constructors.

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</api/language/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 <span class="title-ref">New</span> 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.

For example, this constructor creates a `DateTime</api/data_types/datetime>` instance and sets it to the passed date. The parameters are year, month, and day.

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

If the parameters were omitted, the <span class="title-ref">New</span> date would be set to the current date.

## Sample code

This code creates and then displays a <span class="title-ref">New</span> instance of a `Window</api/user_interface/desktop/desktopwindow>` called "AboutBox":

``` xojo
Var w As AboutBox
w = New AboutBox
```

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

This code creates a <span class="title-ref">New</span> instance of a `DesktopMenuItem</api/user_interface/desktop/desktopmenuitem>` called "WindowItem1":

``` xojo
Var m As DesktopMenuItem
m = New WindowItem1
```

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

This code creates a <span class="title-ref">New</span> instance of a `MessageDialog</api/user_interface/desktop/messagedialog>` box:

``` xojo
Var d As New MessageDialog
```

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

It is equivalent to the following two lines:

``` xojo
Var d As MessageDialog
d = New MessageDialog
```

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

This code uses the `DesktopMenuItem</api/user_interface/desktop/desktopmenuitem>` constructor to create a <span class="title-ref">New</span> menuitem in the Edit menu and assign it its Name property:

``` xojo
Var EditPasteSpecial As New DesktopMenuItem("Paste Special...")
EditMenu.AddMenuAt(5, EditPasteSpecial)
```

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

This code creates a `DateTime</api/data_types/datetime>` instance and sets it to 22 September, 2025, 9:15AM.

``` xojo
Var d As New DateTime(2025, 9, 22, 9, 15, 0)
```

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

The following code is from the example project "Drag and Drop Text" 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 <span class="title-ref">New</span> `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 constructor. The result is a copy of the passed `FolderItem</api/files/folderitem>` rather than a reference to it.

The following code creates a <span class="title-ref">New</span> `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 constructor creates a `Picture</api/graphics/picture>` object that will mirror the content that is drawn into a `DesktopCanvas</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">

`Var</api/language/var>` command; `Nil</api/language/nil>` datatype; `Constructor</api/language/constructor>` concept

</div>
