Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Interface member in a FB

2018-05-11
2018-05-15
  • wpmccormick - 2018-05-11

    I want to have some base class FB as a member of another FB. The way I though this could be achieved was through an Interface. My application compiles and loads, but when I try to access one of the FB/Interface properties I get an exception. Code is something like this:

    INTERFACE ITF_Device EXTENDS __SYSTEM.IQueryInterface
    PROPERTY CID : UDINT

    FUNCTION_BLOCK fbDevice IMPLEMENTS ITF_Device
    PROPERTY CID : UDINT
    GET: CID := THIS^._dParams.ComponentID;

    FUNCTION_BLOCK fbALARM
    VAR
    _device: REFERENCE TO ITF_Device;
    END_VAR
    METHOD GetData
    ComponentNumber := THIS^._device.CID;

     
  • josepmariarams - 2018-05-12

    Who has to implement itf is alarm. Inside alarm you has an reference which points to no anybody.

    As all fb can return his id, instead implement _device you can extend fb_device. Which is not necessary that implements _device. You can treat all your fb as fb_device.

    Be healty!

    Enviat des del meu Aquaris M5.5 usant Tapatalk

     
  • Anonymous - 2018-05-12

    Originally created by: scott_cunningham

    You get an exception because your _device ref variable is never assigned to an actual object. When you use “myvar : REFERENCE TO MyInterface” you are telling the compiler to expect the object to implement the interface MyInterface, but you haven’t actually defined the actual object. These types of errors really only appear when you actually run the project, although you may be able to active a compiler warning - check the options.

    Interfaces are basically a template of what an object is required to code or implement when that object (Finction Block) implements The interface.

    You can use the keyword EXTEND to extend a function block. Then you can use the SUPER^ keyword to access the base FB variables and methods.

     
  • josepmariarams - 2018-05-13

    Hi.

    A question.

    What is _dParams, is an structure that you have created?

    Thanks

    Enviat des del meu Aquaris M5.5 usant Tapatalk

     
  • wpmccormick - 2018-05-14

    Yes, _dParams is a STRUCT.

    I'm sorry, but the language barrier is making it hard for me to understand your earlier reply.

    Maybe you could post a working example?

    Thanks!

     
  • wpmccormick - 2018-05-14

    scott_cunningham hat geschrieben:
    You get an exception because your _device ref variable is never assigned to an actual object. When you use “myvar : REFERENCE TO MyInterface” you are telling the compiler to expect the object to implement the interface MyInterface, but you haven’t actually defined the actual object. These types of errors really only appear when you actually run the project, although you may be able to active a compiler warning - check the options.
    Interfaces are basically a template of what an object is required to code or implement when that object (Finction Block) implements The interface.
    You can use the keyword EXTEND to extend a function block. Then you can use the SUPER^ keyword to access the base FB variables and methods.

    My bad. I didn't include the the part where I assign the object ... but I do. Could the issue be that the FB's and Interfaces' are defined in a library? The help file contains the following: >Zitat:
    The requirement for the explicit conversion is that both the ITF_Source and ITF_Dest are derived from Interface __System.IQueryInterface. This interface is implicitly available does not require a library.

     
  • josepmariarams - 2018-05-15

    I am out and i cannot send an example.

    Simply extend fbalarm with fbdevice.

    Fbalarm extends fbdevice.

    You can erase all references to any interface.

    You can think about interfaces or fb extension more or less:

    Fb extension: when diferents fb have a method with the same code inside and the same name.

    Interface:when diferents fb have a method with the same name and diferent code inside.

    Enviat des del meu Aquaris M5.5 usant Tapatalk

     
  • josepmariarams - 2018-05-15

    _dParams is a system struct or is filled by you?

    Enviat des del meu Aquaris M5.5 usant Tapatalk

     

Log in to post a comment.