Question about object polymorphism – Support Elsist https://support.elsist.biz/forums/topic/question-about-object-polymorphism/feed/ Fri, 30 Oct 2026 08:12:29 +0000 https://bbpress.org/?v=2.6.20 it-IT https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82249 Question about object polymorphism https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82249 Thu, 15 May 2025 09:16:23 +0000 George Is it possible to assign dynamically a FB pointer to an Interface type ?

]]>
https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82251 Rispondi a: Question about object polymorphism https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82251 Thu, 15 May 2025 09:17:55 +0000 Sergio Bertana Yes, also in LogicLab as in many modern IEC 61131-3 environments such as TwinCAT 3, Codesys v3.x, it is possible to dynamically assign a Function Block (FB) to a variable of interface type, provided that the FB implements that interface. The idea is that you can use an interface as an abstract type and dynamically assign an FB that implements it usually through pointer dereferencing (^) or a type cast.

You can download Polymorphism a LogicLab program with an example of use the polymorphism, essentially referring to the screenshot yo can see the Form interface that refer to a trigonometric figure that has two method prototypes.

  • Area: This method calculates the area of the form
  • Perimeter: This method calculates the perimeter of the form

Then two FBs are been defined each implement the Form interface, with the code to calculate the perimeter and area of the relate trigonometric figure. In the FBReference program the RForm variable of Form interface type is istantiated. Then by referencing RForm to different trigonometric figure is possible to calculate perimeter and area of them.

]]>
https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82260 Rispondi a: Question about object polymorphism https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82260 Thu, 15 May 2025 13:25:30 +0000 George If I would instantiate Square and Rectangle FBs dynamically (SysMalloc) as the example

IF (SysFirstLoop) THEN
    MySquare:=SysMAlloc(SIZEOF(Square));
    MyRectangle:=SysMAlloc(SIZEOF(Rectangle));

    @MySquare.Side:=10; //Square side

    @MyRectangle.Width:=20; //Rectangle width
    @MyRectangle.Height:=4; //Rectangle height
END_IF;

is it possible?

]]>
https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82272 Rispondi a: Question about object polymorphism https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82272 Thu, 15 May 2025 15:31:36 +0000 Sergio Bertana I’ve rewritten your program in a form I supposed was correct but there’s two compiling errors.

VAR
    SquareArea : REAL; (* Square area *)
    SquarePerimeter : REAL; (* Square perimeter *)
    RectangleArea : REAL; (* Rectangle area *)
    RectanglePerimeter : REAL; (* Rectangle perimeter *)
    RForm : Form; (* Reference to Form *)
    MySquare : Square^; (* Reference to Square *)
    MyRectangle : Rectangle^; (*  Reference to Rectangle *)
END_VAR

    // Program initializations.

    IF (SysFirstLoop) THEN
//        MySquare:=SysMAlloc(SIZEOF(Square));
//        MyRectangle:=SysMAlloc(SIZEOF(Rectangle));

        MySquare^.Side:=10; //Square side

        MyRectangle^.Width:=20; //Rectangle width
        MyRectangle^.Height:=4; //Rectangle height
    END_IF;

    // Calculates area and perimeter.

    RForm:=MySquare^; //Reference to Form
    SquareArea:=RForm.Area(); //Square area
    SquarePerimeter:=RForm.Perimeter(); //Square perimeter

    RForm:=MyRectangle^; //Reference to Form
    RectangleArea:=RForm.Area(); //Rectangle area
    RectanglePerimeter:=RForm.Perimeter(); //Rectangle perimeter

I’ve to ask to Axel support how to manage this  scenario.

I’ve a question, why you would dynamically instantiate the FBs? Is not easiest simply instantiate they in the data memory?

]]>
https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82315 Rispondi a: Question about object polymorphism https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82315 Fri, 16 May 2025 14:18:03 +0000 George In LogicLab program i write a program like this:

VAR
    SquareArea : REAL; (* Square area *)
    SquarePerimeter : REAL; (* Square perimeter *)
    RectangleArea : REAL; (* Rectangle area *)
    RectanglePerimeter : REAL; (* Rectangle perimeter *)
    RForm : Form; (* Reference to Form *)
    MySquare : Square; (* Square form *)
    MyRectangle : Rectangle; (* Rectangle form *)
    pMySquare : @Square;
    pMyRectangle : @Rectangle;
END_VAR

    IF (SysFirstLoop) THEN
        CASE select OF
            1:
            pMySquare:= SysMAlloc(SIZEOF(Square));
            RForm:=@pMySquare; //Reference to Form
            
            2:
            pMyRectangle:= SysMAlloc(SIZEOF(Rectangle));
            RForm:=@pMyRectangle; //Reference to Form
        END_CASE;
    END_IF;
        
    CASE select OF
        1:
        SquareArea:=RForm.Area(); //Square area
        SquarePerimeter:=RForm.Perimeter(); //Square perimeter

        2:
        RectangleArea:=RForm.Area(); //Rectangle area
        RectanglePerimeter:=RForm.Perimeter(); //Rectangle perimeter
    END_CASE;

In TWINCAT we could write

    pMySquare:= _NEW(Square);
    RForm:= pMySquare^;
    RForm.Area();
    RForm.Perimeter();
]]>
https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82351 Rispondi a: Question about object polymorphism https://support.elsist.biz/forums/topic/question-about-object-polymorphism/#post-82351 Mon, 19 May 2025 06:50:57 +0000 Sergio Bertana Sorry but i don’t understand your needs why you have to dynamically allocate the FBs. You need use them and then you destroy after use? and why you have to destroy them you need to save memory?

To obtain the same result as in the TWINCAT  with LogicLab program you can write:

VAR
    SquareArea : REAL; (* Square area *)
    SquarePerimeter : REAL; (* Square perimeter *)
    RForm : Form; (* Reference to Form *)
    MySquare : Square; (* Square form *)
END_VAR

    MySquare.Side:=10; //Square side
    RForm:=MySquare; //Reference to Form
    SquareArea:=RForm.Area(); //Square area
    SquarePerimeter:=RForm.Perimeter(); //Square perimeter

You simply static instantiate the objects and use them.

]]>