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

Global Variables

yohay
2020-10-22
2020-10-22
  • yohay - 2020-10-22

    Hi,
    I recently start working with Codesys 3.5 insted of 2.3 version.
    In 2.3V I used to declare global vars in the "Global variables" section and then use them anywhere in the code only by their name, but now in the 3.5V I have to call them with an introduction of the Global Variables, for example 'GVL.My_Var' insted of only 'My_Var'.
    It make my code very long and make it hard to read write and debug.
    Am I missing something?
    Why should I use global vars if I have to use this introduction? I can declare local vars and call them with their PRG name, for example 'PLC_PRG.My_Var'.
    Is there any way to declare global variable and call it only by his name all around my code?
    Thanks.

     
  • yohay - 2020-10-22

    Thank you very much!!!
    It help a lot.
    Hope there wont be any problems with that.

     
  • yohay - 2020-10-22

    Forgot to ask, how dose removing qualified_only attribute efect the process?
    It can harm the compiling or the run time of the program?
    What is the purpose of the 'qualified_only attribute'?

     
    • aliazzz

      aliazzz - 2020-10-22

      Removing the

      {attribute 'qualified_only'}
      

      pragma will not affect the code itself but how the compiler should deal with it. Pragma's are IDE or Compiler directives which tell the compiler/IDE how to deal with the code with which the pragma is decorated.

      Basicly this pragma tells you you can only access a variable with a namespace or (fully qualified) or not.

      So

      {attribute 'qualified_only'}
      Gvl.MyGlobal
      

      or when {attribute 'qualified_only'} is removed

      MyGlobal // is valid
      Gvl.MyGlobal // is also valid
      

      As a general rule of thumb qualified only improves readability and is preferred over non qualified referrals.

      For all info on using Pragma's see:
      https://help.codesys.com/api-content/2/codesys/3.5.13.0/en/_cds_using_pragmas/#id3

       
      πŸ‘
      2

      Last edit: aliazzz 2020-10-22
    • Gerhard - 2020-10-22

      Edit: double from aliazzz who explained this as well now☺

      The qualified_only forces you to indicate which global variable list you are using.

      This might be usefull when you have several GVLs.
      When you have several GVLs and the variables also have the same name. The compiler doesn't know which GVL to use and will throw an error.

      A note on your long lines:
      The STweep formatter does support automatic line breaking of long lines. The max line length can set to your own preference.

      https://store.codesys.com/barteling-stweep-formatter-demo.html

       
      πŸ‘
      2

      Last edit: Gerhard 2020-10-22
      • aliazzz

        aliazzz - 2020-10-22

        Just an addition on your excellent remark, search for shadowing rules, as this is what you ment I think.

        This might be usefull when you have several GVLs.
        When you have several GVLs and the variables also have > the same name. The compiler doesn't know which GVL to
        use and will throw an error.

        https://help.codesys.com/webapp/_cds_shadowing_rules;product=codesys;version=3.5.16.0

         

        Last edit: aliazzz 2020-10-22
        • Gerhard - 2020-10-22

          Yep thats what i meant, altough I never read those shadowing rules before. Interesting :)

          I completely disagree with their recommended naming conventions by the way.

           
          • aliazzz

            aliazzz - 2020-10-22

            Naming conventions are always a topic for hot debate, so I won't touch it.
            However, what is very interesting in the Shadowing convention, I think, is this:

            Qualified access can also always be used to avoid shadowing rules.

            The name of the global variable list can be used to uniquely access a variable in the list.
            The name of a library can be used to uniquely access elements in the library.
            The THIS pointer be used to uniquely access variables in a function block, even if a local variable with the same name exists in a method of the function block.

            The above are valid reasons to stick with qualified access.

             

            Last edit: aliazzz 2020-10-22
  • yohay - 2020-10-22

    Thank you for this answer!

     

Log in to post a comment.