Adding a property or method to a class (FB)
In many cases, the scope of methods and properties of a class should be changed. This includes adding new elements as well as changing or removing existing elements. In the following samples, these three procedures are explained using the standard axle type FB_Axis
as an example.
The following steps must be completed in advance for all three procedures:
- 1. Create a new class (FB) and remove
VAR_INPUT
andVAR_OUTPUT.
FUNCTION_BLOCK FB_CustomAxis
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
END_VAR
- 2. Add the class to be inherited to the class definition using the
EXTENDS
keyword.
FUNCTION_BLOCK FB_CustomAxis EXTENDS FB_Axis
VAR
END_VAR
- 3. So that you can also address the class and the added elements via an interface, create an interface with the same name.
INTERFACE I_CustomAxis
- 4. Let the interface inherit from the interface of the inherited class.
INTERFACE I_CustomAxis EXTENDS I_Axis
- 5. Implement the interface in the previously created class (FB).
FUNCTION_BLOCK FB_CustomAxis EXTENDS FB_Axis IMPLEMENTS I_CustomAxis
VAR
END_VAR
Adding a new method/property
- 1. Add a new method/property to the class.
METHOD NewMethod : HRESULT
VAR
END_VAR
- 2. Copy the method into the created interface, if the method should be accessible from outside.
- You have successfully added a new method and can start the implementation.
Extend or overwrite a method/property from the inherited class
- 1. Add a method/property to the class with the name of the method/property to be extended.
METHOD Power : HRESULT
VAR_INPUT
bCommand: BOOL;
END_VAR
VAR
END_VAR
- 2. If you want to extend the method/property rather than overwrite it, you must call the base implementation at the appropriate place.
METHOD Power : HRESULT
VAR_INPUT
bCommand: BOOL;
END_VAR
VAR
nValue: INT;
END_VAR
IF bCommand THEN
nValue := 10;
END_IF
SUPER^.Power(bCommand);
- 3. Check the return value and the
INPUT
variables for consistency. You can view the base implementation by selecting theSUPER^.MethodName()
and then pressing the F12 key. - 4. Implement their own lines of code in the method.
- You have successfully extended a method/property.
Removing a method/property from the inherited class
![]() | Removing a method/property is only possible indirectly Note that you cannot completely remove a method/property! The steps described below only result in the call of the "removed" method/property not causing a reaction. |
- 1. Add a method/property to the class with the name of the method/property to be removed.
- 2. Leave the contents of the method/property empty and do not call the
SUPER^.Method()
.
METHOD Power : HRESULT
VAR_INPUT
bCommand: BOOL;
END_VAR
VAR
END_VAR
- 3. Optional: Add
{attribute „hide“}
to the method/property to hide the method/property in the development environment.
- You have successfully disabled a method/property.