Using pointer to action/method

adrian
2022-03-03
2022-03-03
  • adrian - 2022-03-03

    Hi, it would be possible to pass an action or method as an argument to pou and call it from the pointer?

    I have been trying with ANY type, and i have an adress as a result, but I don't know how to make deference to just do:

    FB myPOU
    VAR_INPUT
    myAct: POINTER TO BYTE;
    END_VAR
    VAR
    pSomethingCallable: POINTER TO ????
    END_VAR

    pSomethingCallable^();

    Thanks in advance!

     
  • ludecus

    ludecus - 2022-03-03

    This why we use the OOP of CODESYS. Simply use a interface instead.

    FB myPOU
    VAR_INPUT
    itfYourInterface : I_YourInterface;
    END_VAR

    itfYourInterface.SomethingCallable();

    Best regards
    ludecus

     

    Last edit: ludecus 2022-03-03
    • adrian - 2022-03-03

      thanks for answering, but what I'm trying to do is to create a fb that have other fb inside, the first fb also haves a method that should be called from de second fb, so I need to pass the method itself because I didnt't know how it would be called or even how the fb myPou is gonne be.
      Thanks!

       
      • hermsen

        hermsen - 2022-03-03

        I misunderstood the question.
        Interfaces are still handy (learn to use them) so you will understand Ludecus's anwser more clearly.

         
        πŸ‘
        1

        Last edit: hermsen 2022-03-03
      • ludecus

        ludecus - 2022-03-03

        Whoho! If i get you right, it's still Object-oriented programming. Please inform yourself, adrian!

        Adrian, you can't pass a method or an action like that.

        If the second fb owns the first fb, it will look like that:

        FUNCTION_BLOCK SecondFB
        VAR
            firstFb     : FirstFb;      // local instance of the child class
        END_VAR
        
        // call a method of the child
        firstFb.SomethingCallable();
        

        If the second fb doesn't know the first fb, you will need an interface.

        FUNCTION_BLOCK FirstFb IMPLEMENTS I_FirstFb
        
        FUNCTION_BLOCK SecondFB
        VAR_INPUT
            ifirstFb    : I_FirstFb;    // reference to the first fb
        END_VAR
        
        // call a method of the reference
        ifirstFb.SomethingCallable();
        
         

        Last edit: ludecus 2022-03-03
      • fajean - 2022-03-03

        Although you cannot pass the address of a method and there are no lambda expressions in CODESYS, you can pass a pointer/reference of the outer function block to the inner function block, or pass the parent FB in the form of an instance of an interface the outer function block implements. Although it may not be your preferred idiom, I am not sure why you think it would not work. And here by "pass", I mean do it ahead of time (could be using FB_Init) so that the inner FB can keep that pointer/reference and use it at any later time.

         

        Last edit: fajean 2022-03-03
        • hermsen

          hermsen - 2022-03-03

          Isn't this called "dependency injection"?

           

          Last edit: hermsen 2022-03-03
          • fajean - 2022-03-03

            I guess it is! Passing methods by address or using lambda expression (which is a nicer way to to something similar) also would be, if not rigorously speaking, at least from a modelling point of view. I am not a scholar, I have just been programming for decades in various languages, so there are concepts that are entirely clear in my mind for which I do not use the vocabulary from textbooks. I write code, not slides or presentations, the compiler/interpreter is my audience... :)

             
  • hermsen

    hermsen - 2022-03-03

    1) "You can't pass a method or an action like that."
    => Obviously, but Interfaces are very neat and flexible and hold the key to solve this "problem". I simply explained what the basic mechanism of an interface is, rather then explain the correct usage in this specific case to solve the issue. I don't think that the user gains anything from Copy/Paste solutions if he/she does not understand the basics.

     

    Last edit: hermsen 2022-03-03
  • adrian - 2022-03-03

    Ok, seeing your answers I think I can't do what I wanted to do.
    I came from Python where this things are daily basics, sorry for asking something out of the codesys world.
    Interfaces are good and I used it a lot, but it are meant to work with related objects, what I was trying to do is a multiple uses block, something that maybe I could post as a library. Thanks anyway!

     
  • adrian - 2022-03-03

    Ok, seeing your answers I think I can't do what I wanted to do.
    I came from Python where this things are daily basics, sorry for asking something out of the codesys world.
    Interfaces are good and I used it a lot, but it are meant to work with related objects, what I was trying to do is a multiple uses block, something that maybe I could post as a library. Thanks anyway!

     
  • adrian - 2022-03-03

    Ok, seeing your answers I think I can't do what I wanted to do.
    I came from Python where this things are daily basics, sorry for asking something out of the codesys world.
    Interfaces are good and I used it a lot, but it are meant to work with related objects, what I was trying to do is a multiple uses block, something that maybe I could post as a library. Thanks anyway!

     
    • hermsen

      hermsen - 2022-03-03

      "I came from Python where this things are daily basics" => ? AFAIK Python doesn't implement Interfaces. It only sports classes which implement methods/properties direct.

      "sorry for asking something out of the codesys world" =>
      Why are you sorry?

      "What I was trying to do is a multiple uses block" => Please try to explain your question more thorough, if you can explain it well, we can surely help you find an anwser.

      Besides that, I strongly advise against building a "multiple uses block" as it surely goes against the SOLID Single responsibility principle of ONE object should only do ONE task(!)

       
      πŸ‘
      1

Log in to post a comment.