Extending a function block
The extension of a function block is based on the concept of inheritance in object-oriented programming. A derived function block "extends" a basic function block for this purpose and thus basically obtains ("inherits") the properties and functionalities of the basic function block – in addition to its own properties and functionalities.
In TwinCAT, the term "function block" can be used interchangeably with "class". A derived function block can therefore be referred to as "subclass", and the basic function block as "base class".
Observe the following information:
Number of extensions for each basic function block The number of extensions for each basic function block is unlimited. A function block an therefore be extended and customized with several other function blocks.
|
Number of inheritance levels The number of inheritance levels is unlimited. A function block that extends another function block can therefore by customized with a further function block, etc.
|
Multiple inheritance is not allowed Multiple inheritance is not allowed for function blocks. A function block cannot extend more than one other function block. Exception: A function block can implement several interfaces and an interface can extend several other interfaces.
|
Overloaded Overloading of methods is not possible. Therefore, if you overwrite or extend a base class method (same method name) in a subclass, the method declaration must match the base class declaration (access modifier, return type, variable interface). |
Extending a basic function block with a new function block
- The currently open project has a basic function block, for example FB_Sample, which is to be extended with a new function block.
- 1. Select the PLC project object or a subfolder in the PLC project tree, and in the context select the command menu Add > POU...
- The Add POU dialog opens.
- 2. Enter a name for the new function block in the Name input field, for example FB_SampleEx.
- 3. Select Function Block.
- 4. Select Extends and click on .
- 5. In the input assistant, from the Functionblocks category under the project select the POU(FB) to be used as basic function block, for example “FB_Sample”, and click OK.
- 6. Optionally you can select an Access specifier for the new function block from the combo box.
- 7. From the Implementation language select “Structured Text (ST)”, for example.
- 8. Click on Open.
- TwinCAT adds the function block FB_SampleEx in the PLC project tree, and the editor opens. The first line says:
FUNCTION_BLOCK FB_SampleEx EXTENDS FB_Sample
The function block FB_SampleEx extends the basic function block FB_Sample.
Extending a basic function block with an existing function block
- The currently open project has a basic function block, for example “FB_Sample”, and a further function block, for example “FB_SampleEx”, which is not yet derived from the basic function block. The function block FB_SampleEx is to extend the basic function block, i.e.: FB_SampleEx is to extend FB_Sample.
- 1. Double-click on the function block FB_SampleEx in the PLC project tree.
- The POU editor opens.
- 2. Extend the existing entry in the top row, FUNCTION_BLOCK FB_SampleEx, with EXTENDS FB_Sample.
- The function block FB_SampleEx extends the basic function block FB_Sample.
See also:
- Object Function block
- Object Property
- Object Method
- Object Action
- Object Transition
- Reference Programming: SUPER
- Reference Programming: THIS