Object ‘Method’

Symbol:

Keyword: METHOD

Methods are an extension of the IEC 61131-3 standard and a tool for object-oriented programming which is used for data encapsulation. A method contains a declaration and an implementation. However, unlike a function, a method is not an independent POU, and it is subordinated to a function block or program. A method can access all valid variables of the superordinate POU.

You can use interfaces for the organization of methods.

You can add a method below a program or a function block. Click Project ‣ Add Object ‣ Method to open the Add Method dialog.

Declaration

  • The variables of a method contain temporary data that are valid only during the execution of the method (stack variables). All variables that are declared and implemented in a method are reinitialized each time the method is called.
  • Like functions, methods can have additional outputs. You must assign these additional outputs in the method call.
  • Depending on the declared access specifier, a method can be called only within its own namespace (INTERNAL), only within its own POU and its derivatives (PROTECTED), or only within its own POU (PRIVATE). For PUBLIC, the method can be called from anywhere.

Interface methods can have declared input, output, and VAR_IN_OUT variables, but do not contain an implementation.

See also

Implementation

  • Access to function block instances or program variables is permitted in the implementation of the method.
  • The THIS pointer allows for access to its own function block instance. Therefore, the pointer is permitted only in methods that are assigned to a function block.
  • A method cannot access VAR_TEMP variables of the function block.
  • A method can call itself recursively.

Hint

When you copy a method below a POU and add it below an interface, or move the method there, the contained implementation is removed automatically.

Calling a method

Syntax for calls:

<return value variable> := <POU name> . <method name> ( <method input name> := <variable name> (, <further method input name> := <variable name> )* );

For the method call, you assign transfer parameters to the input variables of the method. Respect the declaration when doing this. It is enough to specify the names of the input variables without paying attention to their order in the declaration.

Example

Declaration

METHOD PUBLIC DoIt : BOOL
VAR_INPUT
        iInput_1 : DWORD;
        iInput_2 : DWORD;
        sInput_3 : STRING(12);
END_VAR

Call

bFinishedMethod := fbInstance.DoIt(sInput_3 := 'Hello World ', iInput_2 := 16#FFFF, iInput_1 := 16);

When the method is called, the return value of the method is assigned, for example, to variables declared locally. When you omit the names of the input variables, you have to pay attention to the declaration order.

Example

Declaration

METHOD PUBLIC DoIt : BOOL
VAR_INPUT
        iInput_1 : DWORD;
        iInput_2 : DWORD;
        sInput_3 : STRING(12);
END_VAR

Call

bFinishedMethod := fbInstance.DoIt( 16, 16#FFFF,'Hello World ');

Recursive method call

Within the implementation, a method can call itself, either directly by means of the THIS pointer, or by means of a local variable for the assigned function block.

  • THIS^. <method name> ( <parameter transfer of all input and output variables> )

    Direct call of the relevant function block instance with the THIS pointer

  • VAR fb_Temp : <function block name>; END_VAR

    Call by means of a local variable of the method that temporarily instantiates the relevant function block

A compiler warning is issued for a recursive call. If the method is provided with the pragma {attribute 'estimated-stack-usage' := '<sstimated_stack_size_in_bytes>'}, then the compiler warning is suppressed. For an implementation example, see the “Attribute ‘estimated-stack-usage’” chapter.

To call methods recursively, it is not enough to specify only the method name. If only the method name is specified, then a compiler error is issued: Program name, function or function block instance expected instead of

See also

Special methods of a function block

FB_Init

Declarations automatically implicit, but explicit declaration also possible

Contains initialization code for the function block, as is defined in the declaration part of the function block

FB_Reinit

Explicit declaration is necessary.

Call after the instance of the function block was copied (as during an online change). It reinitializes the new instance module.

FB_Exit

Explicit declaration is necessary.

Call for each instance of the function block before a new download or a reset or during an online change for all shifted or deleted instances.

Properties Provides Set and/or Get accessor methods.

See also

Dialog ‘Add Method’

Function: Defines a method below the selected POU when the dialog is closed.

Call: Menu bar: Project ‣ Add Object ‣ Method ; context menu

Requirement: A program (PRG) or a function block (FUNCTION_BLOCK) is selected in the POUs view or the Devices view.

The interface of a method inserted below a basic function block is copied when a method with the same name is inserted below a derived function block.

Name

Example: meth_DoIt.

The standard methods FB_Init and FB_Exit are offered in a list box if they are not already inserted below the POU. If it is a derived function block, then the list box also offers all of the methods of the basic function block.

Return type

Default data type or structured data type of return value

Example: BOOL

Implementation language Example: Structured Text (ST)
Access specifier

Controls access to data.

  • PUBLIC or not specified: Access is not restricted.

  • PRIVATE: Access is restricted to the program, function block, or GVL.

    The object is marked as (private) in the POU or device view. The declaration contains the keyword PRIVATE.

  • PROTECTED: Access is restricted to the program, function block, or GVL with its derivations. The declaration contains the keyword PROTECTED.

    The object is marked as (protected) in the POU or device view.

  • INTERNAL: Access to the method is restricted to the namespace (library).

    The object is marked as (internal) in the POU or device view. The declaration contains the keyword INTERNAL.

Abstract : Identifies that the method does not have an implementation and the implementation is provided by the derived FB
Add Adds a new method below the selected object.

Input assistance when creating inheriting blocks

When you do object-oriented programming and want to use inheritance for blocks, you have the following support: When you insert a method, action, etc. below an inherited block, the Add Object dialog box includes a combo box with a list of methods, actions, etc. used in the base block. In this way, you can easily accept a method definition of the base and adapt it accordingly for the inherited method of the block. Methods and attributes with the PRIVATE access modifier are not available in this selection because they should not be inherited. When accepted into the inherited block, methods and attributes with the PUBLIC access modifier automatically have a blank access modifier field. (Functionally, this means the same thing.)

See also

See also