2.22 :init()

The :init() method is a predefined, overwritable method that is always invoked internally after the creation of an object. The method can be overwritten to define an own initialization. The :init() method enables to influence the sequence of initialization, e.g. initializing the parent object before its children or performing a partial initialization of the parent, then initialize its children and finally perform the remaining initialization of the parent.

The :init() method cannot be invoked directly. It provides preset parameters that can be accessed within the method. The method does not have a return value (i.e. void).

Definition

void :init
(
  object  Model     input,
  object  Parent    input,
  object  Module    input,
  integer Type      input,
  boolean Invisible input
)

Parameters

The predefined parameters are preset as follows:

object Model input
Is set to this.model.
object Parent input
Is set to this.parent.
object Module input
Is set to this.module.
integer Type input
Is set to this.scope.
boolean Invisible input
Is set according to the respective parameter of the create() call.

Redefinition

The method can be redefined directly at the object.

:init()
{
  // Rule Language code
}

Please note that neither the predefined parameters are indicated nor additional parameters may be defined when redefining the method.

Invocation

The method cannot be called directly. It can be triggered only either by loading a module or dialog or by creating new objects dynamically with the functions create() or DM_CreateObject(). Direct invocations are ignored or rather lead to errors.

Event Processing After Loading

The :init() method is executed before the start rule of the dialog. Possible events are ignored and therefore not processed. This means that changed events are not processed either and no on changed rules are triggered.

this:super()

Using Model Methods

Frequently, initialization methods are defined at the model and those shall be executed as well. They can be invoked by calling this:super() within the :init() method of an instance. Please note that this:super() is always called without parameters.

Controlling Initialization of the Children

If no more model method exists, calls of this:super() invoke the :init() methods of the children. When there are no children, the call returns without error. When the children have no :init() methods, the :init() methods of the children's children are invoked. In this way it can be controlled whether and when the children are initialized. Please note, however, that the children of the dialog are not controlled by this:super().

Important Note

Using fail() and pass this:super() in the :init() method should be absolutely avoided. In either case, occurring errors are no longer output to the trace or log files (output of ***ERROR IN EVAL is ceased). This hinders error detection considerably up to preventing it completely.

Example 1

dialog D1

model window MW
{
  :init()
  {
    static variable integer I := 1;
    !! take only instances into account
    if Type = 3 then
      this.title := "Window No. " + I;
      I := I + 1;
    endif
  }
}

Example 2

dialog D2

model record MRecWin
{
  !! some important data
  string Important[100];
}

model window MW
{
  !! store the corresponding record here
  object MyRec;
  :init()
  {
    this.MyRec := create(MRecWin, D);
  }
}

Remark

The previous example serves just for illustration. It may be expressed much easier:

dialog D2_Improved

 model window MW
{
  record MyRec
  {
    !! some important data
    string Important[100];
  }
}

Example 3

dialog D3

model window MW
{
  child groupbox G
  {
    :init()
    {
      !! do something with G
    }
  }
  :init()
  {
    !! do something before :init() of the children is invoked
    !! ...
    this:super();
    !! do something after :init() of the children has been executed
    !! ...
  }
}

Example 4

dialog D4

model window MW1
{
  child groupbox G
  {
    :init()
    {
      !! do something with G
    }
  }
  :init()
  {
    if Type = 3 then
      !! G is initialized only if the this object is no Model
      this:super ();
    endif
  }
}

model MW1 MW2
{
  :init()
  {
    !! do something with the object, here the method of MW1 is invoked
    this:super ();
    !! further operations on the object
  }
}

Example 5

dialog D5

model groupbox MG
{
  :init()
  {
    print Parent;
    print Model;
    print Module;
    print Type;
    print Invisible;
  }
}
window W
{
}
on dialog start
{
  MG:create(W);
}

Output

W
MG
D5
3
false