[OOP] How to implement "HAS A"... that is, instance of class within a class

jcpershe
2017-04-28
2017-06-07
  • jcpershe - 2017-04-28

    I have created a class (FB with Methods and Properties).

    I would like one the variables within this class to be another class (FB with Methods and Properties).

    But when I try to use F2 to enter a specific method or property of the instance within the instance, I cannot find the instance within.

    I would like to access the elements something like:
    ourLargeSystem[3].mySubSystem.mySubInt := 4;

    This doesn't seem to work. I get an error that 'mySubSystem' is not an input of largeSystem.

    Here's some pseudo-code. What I am doing wrong.

    Thanks for any ideas. I must be missing something.
    JP

    GlobalVariables
    ourLargeSystems : ARRRAY[1..10] OF largeSystem;
    END Global Variables

    FB largeSystem
    VAR
    _myInt : INT;
    _myReal : REAL;
    mySubSystem : subSystem

    END_VAR
    etc... with Methods and Properties

    FB subSystem
    VAR
    _mySubInt : INT;
    END_VAR
    etc. with Methods and Properties

     

    Related

    Talk.ru: 3

  • Anonymous - 2017-04-28

    Originally created by: scott_cunningham

    Your problem is scope of variables. Items defined as VARs of FBs are not available outside of the FB.

     
  • josepmariarams - 2017-04-28

    If you want to acces variables outside fb you habe to declare its as var_input.

    But is a best practice to acces its via methotds (get set).

     
  • chris.beard - 2017-04-29

    For true OOP you should be able to make the new fb with the extends or implements setting and then also use the super command too see the following as a good example. https://infosys.beckhoff.com/english.ph ... 9.html&id=

    Sent from my SM-N9005 using Tapatalk

     
  • jcpershe - 2017-05-10

    Josep M. Rams hat geschrieben:
    If you want to acces variables outside fb you habe to declare its as var_input.
    But is a best practice to acces its via methotds (get set).

    Thank you. That was the issue. I plan to use the methods for the member instances, but I could not "see" the member methods and properties until I moved the variable to VAR_INPUT.

    Thanks.
    jcp

     
  • Anonymous - 2017-05-12

    Originally created by: scott_cunningham

    You can also define the object as a VAR in your function block and then create a property for your function block that returns a reference to the object. Only create the get - delete the set. You can then have access to the internal object's methods and properties.

     
  • Anonymous - 2017-06-07

    Originally created by: scott_cunningham

    Here is a screen shot of your solution using a property and REFERENCE TO. You have access to all of subsystem's methods and properties. You do not have access to subsystem's internal VARs like mySubInt (the only debugger does, however). I used the property CurrentCount for that. (You could have a Set function for CurrentCount, and then you basically have access to your internal var).

    IMG: has

     

Log in to post a comment.