REFERENCE TO

gwenael
2021-10-31
2021-11-02
  • gwenael - 2021-10-31

    Hello,

    I have a FB with 2 REFERENCE TO FB as input parameters.
    In the FB, i want to work with either the first or the second Reference depending on a variable (NumSupport)
    But when I try to affect the RefSupport:=RefSupport1, I get an exception.
    Does anyone know how to deal with this problem ? What is correct way to work ?

    Thank you

    FUNCTION_BLOCK FB_Manip
    VAR_INPUT
    RefSupport1 : REFERENCE TO FB_Support;
    RefSupport2 : REFERENCE TO FB_Support;
    END_VAR
    VAR
    RefSupport : REFERENCE TO FB_Support;
    NumSupport: INT:=1;
    END_VAR

    IF NumSupport=1 THEN
    RefSupport:=RefSupport1;
    ELSE
    RefSupport:=RefSupport2;
    END_IF

     
  • hermsen

    hermsen - 2021-10-31

    The operator generates a reference (pointer) to a value;

       RefSupport REF= RefSupport1;
    

    see https://help.codesys.com/webapp/_cds_ref_assignment;product=codesys;version=3.5.17.0

    TIP: use a CASE statement for NumSupport instead of a IF THEN, that way you can easily extend your code in the future i.e;

    CASE NumSupport OF
    
        1: ;
    
        2: ;
    
    ELSE
        ;
    
    END_case;
    
     
    πŸ‘
    1

    Last edit: hermsen 2021-10-31
    • gwenael - 2021-10-31

      so easy ;)
      Thank You !!!!

       

Log in to post a comment.