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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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'?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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//isvalidGvl.MyGlobal//isalsovalid
As a general rule of thumb qualified only improves readability and is preferred over non qualified referrals.
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.
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.
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
When you make a GVL, it automatically adds the qualified_only attribute. (See screenshot)
You can remove it.
https://help.codesys.com/webapp/_cds_pragma_attribute_qualified_only;product=codesys;version=3.5.16.0
alternatively, there is such products:
https://www.lg.com/us/monitors/lg-29wn600-w-ultrawide-monitor
you can call your GVL "g", so that it is not as long.
Last edit: i-campbell 2020-10-22
Thank you very much!!!
It help a lot.
Hope there wont be any problems with that.
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'?
Removing the
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
or when {attribute 'qualified_only'} is removed
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
Last edit: aliazzz 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
Last edit: Gerhard 2020-10-22
Just an addition on your excellent remark, search for shadowing rules, as this is what you ment I think.
https://help.codesys.com/webapp/_cds_shadowing_rules;product=codesys;version=3.5.16.0
Last edit: aliazzz 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.
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:
The above are valid reasons to stick with qualified access.
Last edit: aliazzz 2020-10-22
Thank you for this answer!