Project members: Method

With a method member you can implement the common behavior (the logic) of a component. The methods are represented in Composer by following bricks:

Every method has a body containing its implementation code. The code is expressed in the platform-independent Chora syntax. Within the code you can evaluate and modify variables, perform calculations, call other methods, etc. Optionally a method can specify a list of parameters and its return value. Through these you can pass values to the method when it is called and receive the result when it returns.

A method can be defined within a class or class variant only. If one class is derived from another class, all methods implemented in the ancestor classes are inherited implicitly. In the derived class the inherited methods can be overridden and modified with new implementation.

In order to preserve the clarity, all methods inherited from a base class but not overridden in the actual class are not shown as bricks in the Composer. The Inspector, in turn, shows always the complete list including all inherited members. If you are looking for a particular inherited method, use the Inspector for this purpose. Please note, inherited but not overridden methods are shown in Inspector with gray thin font:

Inherited but not overridden methods appear only in the Inspector window. They are shown with thin gray font.

If an inherited method has been overridden, Inspector shows it with dark gray font. Additionally, overridden methods appear as bricks in the Composer window. You can distinguish easily, whether a method is overridden or whether it is new by considering the font color in its brick. New methods are shown with black font. Overridden methods, in turn, with gray font:

Overridden methods appear in Inspector with dark gray font. They are also shown as bricks in Composer, again with gray font.

Add new method

First switch to the Composer page for the respective class or class variant definition, where you want to add the new method.

Then, in the Gallery window, ensure that the folder Chora is opened.

Look in the folder for the template named Method.

With the mouse, select the template and drag it into the Composer.

Drop the template within the Composer.

Adding a new method.

Name the method

First ensure, that the method is selected.

Press the key F2 or select the menu item EDITRename ....

Enter the new name in the Inspector window.

Methods have a local character - they can be accessed only in context of an instance of the class, in which they are defined. To address a method, you use its name. Please note, the name of an inherited method can't be changed retrospectively in the derived class.

Open the method for editing in Editor window

The method is edited in the Code Editor window. For this purpose:

First ensure, that the method is selected.

Press the key ENTER ...

... or double click on the method with the mouse.

Specify the method return data type

A method can be implemented to return a value to its caller. Thereupon the caller can evaluate the resulting value within expressions. If you intend to implement a method able to return a value, you have to explicitly specify its data type in the declaration area of the Code Editor window.

To inspect and edit the return type open the method in the Code Editor first.

Then in the declaration area specify the desired data type.

Please note, new added methods are per default declared with int32 return data type. If the method is not intended to return any value to the caller, you should declare it with void as its return data type. Moreover, the declaration of an inherited method can't be changed retrospectively in derived classes.

Specify the method parameters

A method can be implemented to receive parameters from its caller. The parameters can thus be evaluated within expressions in the implementation of the method. All method parameters including their data types must be explicitly specified in the declaration area of the Code Editor window. This declaration is binding for the caller. In other words, the method must be called with arguments matching exactly the declaration in respect to the number of arguments and their individual data types. Method declared without parameter is not able to receive any arguments from its caller.

To inspect and edit the parameters open the method in the Code Editor first.

Then use the Code Editor to add new and edit, duplicate, reorder, delete existing parameters.

Please note, new added methods are per default declared with a single int32 parameter. Moreover, the declaration of an inherited method can't be changed retrospectively in derived classes.

Edit the method implementation

The method implementation (its body) is edited in the editor area of the Code Editor window. You use Chora statements and expressions to formulate your intention, what the method should do.

Please note, before you can edit an inherited method, you have to override it first.

Return a value

If the method is declared with a return data type other than void, the method must always return a value matching this type declaration. To return values you use in the implementation of the method the return statement.

Evaluate the method parameters

Parameters can be evaluated within Chora expressions wherever an operand with the data type matching the parameter's declaration is expected. Moreover, the parameters can be modified by assigning them new values. This, however, has a local effect in the context of the currently executed method only. Changing a parameter within the method doesn't affect the caller's original argument.

Parameters are addressed by using the names specified in their declaration. For example, a method intended to estimate and return the minimum of two int32 parameters named literally aValue1 and aValue2 could be implemented as follows:

if ( aValue1 < aValue2 )
  return aValue1;
else
  return aValue2;

Duplicate an existing method

You can create any number of copies of an already existing method.

First ensure, that the method is selected.

Press the keys CtrlC and CtrlV in succession ...

... or hold the key Ctrl pressed while you drag and drop the selected method.

Finally rename the just duplicated method.

Override an inherited method

In order to provide new implementation for an inherited method, you have to override it explicitly:

First ensure, that the method is selected in the Inspector window.

Drag and drop the selected method from the Inspector to the Composer window while holding the keys CtrlShift pressed.

Finally you can open the method for editing.

Overriding an inherited method.

Perform a super() call

When you override an inherited method, it is often convenient to invoke from the new implementation the inherited version of the method. This can be achieved by calling the pseudo method super() with arguments matching exactly the declaration of the actual method. If the method is declared to return a value, the result of the super() call can also be evaluated. For example:

var rect area = ...;

// Let's assume the current method is declared with a 'rect' and
// a 'bool' parameters. Then following is correct to call its
// inherited version.
super( area, false );

Revert to inherited method

If you want to revert an overridden method to its original inherited implementation, delete it simply. Thereupon, its brick disappears from the Composer and the Inspector shows the method name with thin font again.

Use the method

Methods can be called from another methods, or they can be involved in Chora expressions wherever operands matching the method's return type declaration are expected. To call a method you use its name followed by a pair of () (parenthesis). When calling the method, the caller can provide arguments, which have to match exactly the method's declaration in respect to the number of arguments and their individual data types.

Methods are always called in scope of the object, the method belongs to. If no scope is specified, the operation is considered as being performed in context of this object. For example:

// Call a method in context of 'this' object
InvalidateViewState();

var Example::DeviceClass device = ...;

// Call methods of a foreign object
if ( device.IsEngineRunning( 4 ))
  device.StartEngine( 4 );

Control the code generation

With the attribute Generator you can selectively control, whether the method should be taken in account during the code generation or not. Configuring this attribute with the value false will exclude the method from your project unless the corresponding method is explicitly called from another method or it is involved within a Chora expression.

Delete a method

First ensure, that the method member is selected.

Press the key DEL or select the menu item EDITDelete.

Please note, inherited methods can't be deleted. However, if a method is overridden you can delete this overriding.

Attributes of a method

The following table shows the complete list of attributes provided by the method member:

Attribute name

Short description

Brick

Determines the position and the size of a method member brick within the Composer window.

Description

Contains the description for the method member.

Generator

Controls the code generation for the affected method member.

Native

Specifies the kind of language the corresponding method is implemented in.