New

From Xojo Documentation

Operator

Used to create new instances of objects at runtime.

Usage

The New operator has several different syntaxes:

When instantiating new instances of controls:

object reference = New ObjectName

Part Type Description
object reference Any object type Required; variable of type object name.
object name Object name Required. Any name of an object in the current window.

When instantiating new instances all other types of objects:

object reference = New ObjectClass | ControlArrayName [(param1, param2,...)]

Part Type Description
object reference Any object type Variable of type object class.
object class Object name Name of any object class other than the control classes.
ControlArrayName Object name The name of any control in the current window.
param1...n Any Optional parameters passed to the object or control array.

When creating and instantiating instances of all other types of objects:

Dim ObjectReference As New ObjectClass | ControlArrayName [param1, param2,...paramN]

Part Type Description
ObjectReference Any object type Required; variable of type object class.
ObjectClass Object name Required. Name of any object class other than the control classes.
ControlArrayName Object name The name of any control in the current window.
param1...n Any Optional parameters passed to the object or control array.

Notes

The constructor for a class is called whenever you create a new instance of it with the New statement.

The New operator can appear as a modifier on a Dim statement that creates a new object. This usage takes the place of two lines of code: The Dim statement that creates the object and a separate New statement that instantiates the object.

Creating new instances of controls or menu items

The New operator can only create a new instance of a control that already exists in the window or a menu item that already exists on a menu. The object name acts as a template for creating the new instance of the control or menu item. The New operator works if the control or menu item is in a control array. The object reference variable must be defined as type object name. For more information, refer to User Guide Book 2: User Interface, Chapter 1: Desktop, Section 16: Control Sets and Dynamic Controls.

Creating new instances of all other types of objects

The New operator can be used to create a new instance of any type of object (for controls see the notes above). The object reference must be defined as being of type object class.

If the class's Visible property is set to True, the new instance will appear visibly in the running application.

Constructors

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.

For example, this constructor creates a Date instance and sets it to the passed date. The parameters are year, month, and day.

Dim d As New Date(2011, 4, 15)

If the parameters were omitted, the new date would be set to the current date.

Sample Code

This code creates a new instance of a pushbutton called "Pushbutton1" then sets its caption. Add a PushButton to a window and name it PushButton1. Set its Index property to zero, making it the first element in a control array. Set its Caption property to "Original".

In its Action event, create a clone of the pushbutton and enter this code

Dim pb As PushButton
pb = New PushButton1
pb.Caption = "Clone"
pb.Left = Me.Left + Me.Width + 10

When you run the application and click the pushbutton, it creates a new pushbutton to its right that is the second element of the control array. You can tell by its caption that it is the new pushbutton that was created in the Action event of the original PushButton.

This code creates and then displays a new instance of a Window called "AboutBox":

Dim w As AboutBox
w = New AboutBox

This code creates a new instance of a MenuItem called "WindowItem1":

Dim m As MenuItem
m = New WindowItem1

This example creates a new instance of a MessageDialog box:

Dim d As New MessageDialog

It is equivalent to the following two lines:

Dim d As MessageDialog
d = New MessageDialog

This code uses the MenuItem constructor to create a new menuitem in the Edit menu and assign it its Name property:

Dim EditPasteSpecial As New MenuItem("Paste Special...")
Editmenu.Insert(5, EditPasteSpecial)

Constructors

This example creates a Date instance and sets it to 22 September, 2011, 9:15AM.

Dim d As New Date(2018, 9, 22, 9, 15, 0)

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:

Dim d As DragItem
d = New DragItem(Self, Me.Left, Me.Top, Me.Width, Me.Height)
d.Text = Me.Text
d.Drag

The following code 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 instance that has an alpha channel (i.e., a transparency parameter).

Dim width As Integer = 2000
Dim height As Integer = 2000

// Creates new picture
Dim pic As New Picture(width, height)

This constructor creates a Picture object that will mirror the content that is drawn into a Canvas in its Paint event.

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

See Also

Nil datatype; Constructor.