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 .
★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.
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 );
Please note, starting with version 13 each newly overridden method contains code to perform the super() invocation by default. If it is not desired, you can select and delete the code fragment.
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 .
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 |
---|---|
Determines the position and the size of a method member brick within the Composer window. |
|
Contains the description for the method member. |
|
Controls the code generation for the affected method member. |
|
Specifies the kind of language the corresponding method is implemented in. |
Chora syntax of a method
This section describes the Chora syntax of how methods are stored inside Embedded Wizard project files. The knowledge about the syntax can be helpful when you plan to process the project contents by third party tools. In particular, the general purpose Export and Import functionality introduced in version 14 requires a good understanding of how project members are represented in Chora syntax to correctly interpret the exported members and synthesize the Chora code for members to import. Please note, Embedded Wizard project files (and thus Chora code) are encoded as text in UTF-8 format.
In Chora syntax, method definition is always introduced with the keyword method, followed by the data type of the method's return value, the name of the method, a list of method arguments and the body (the implementation) of the method. The list of method arguments is enclosed between a pair of parenthesis (..), whereby each arguments starts with the keyword arg followed by the data type of the argument and its name. Methods not intended to return any value use the return data type void. If the method is not intended to receive any parameters, the list between the parenthesis (..) remains empty. The body of the method is enclosed between a pair of curly braces {..}. In the implementation of the method the Chora statements are used. Wherever necessary, the tokens are separated by whitespace signs.
The definition of a method can be initiated by a comment block providing optional description for the method. The content of this comment corresponds to the value of the methods's attribute Description. Furthermore the directives $rect and $output can be specified providing values for the corresponding attributes Brick and Generator.
In case of a method inherited from a base class, the method definition is additionally prefixed by the keyword inherited. In this case the method inherits its return data type, the parameter list and the value for the $output directive from the base class. Modifying these values in the inherited variable is not foreseen. The following examples demonstrate the described syntax:
// This is a method not returning any values nor expecting parameters. \ // // $rect determines the position of the brick inside Composer window. $rect <10,10,210,50> method void UpdateStatus() { /* Some code to perform the update */ } // This is a method intended to perform some calculation. It expects two \ // parameter and returns a value. // // $rect determines the position of the brick inside Composer window. // $output imposes the code generation of the method regardless of its \ // usage inside the project. $rect <10,10,210,50> $output true method int32 Add( arg int32 aArg1, arg int32 aArg2 ) { return aArg1 + aArg2; } // This is a method inherited from a base class. Its return data type \ // and the parameter list are determined in the base class and can't be \ // changed in descending classes. // // $rect determines the position of the brick inside Composer window. $rect <10,10,210,50> inherited method UpdateLayout() { super( aSize ); SomeView.Bounds.size = aSize; }
Each Chora file (or text exchanged with an external Export and/or Import tool) can be (or even should be) prefixed with $version directive. This directive determines the version number of the included Chora code. For details concerning the syntax of the version please see the chapter $version macro. Furthermore, the code can include a $scope directive followed by a path to a project member acting as the original owner of the corresponding code. That means the members described by the Chora code existed inside the member specified in $scope. In case of a method, $scope would specify the name of the class the method belongs to.