Unofficial Nextion/TJC User Forum

Two global variables sharing the same name

I found it out the hard way: it is possible to define two variables in different pages as global with the same name. That should not be possible. The behaviour is somewhat unpredictable, especially if they are accessed from within a third page. So, for example, you define a global variable named “once” on page “main” and use the same variable name “once” for a global variable on page “service”. If you then manipulate once.val from within service without prefixing it like “service.once.val” then you might up ending changing “oncle.val” of page “main”.

I was astonished that is even possible and it took me quite a while to track down, because it is very easy to loose track in a larger project (>3500 lines of code) due to the cumbersome GUI, which is nice for smaller projects only.

Maybe somebody can comment on whether this is a “bug” or a “feature”?

Global variables always need to have the page reference unless that page is active. I would say you should never use the same name for a local variable and a global variable whether or not the local variable is also global.

3500 lines of code is a small project :smiley: Each page in my project takes at least 1000 lines of code and there are 23 pages. The mcu side has >25000 lines.

I remember testing the behaviour of global variables some time ago. My experience was that global variables cannot be referenced from a different page without the pagename. If you omit pagename it will always refer to the object on the current page according to my experience. I have NX8048.

So I thought it is safe to have two global variables with the same name, and always refer to them with pagename included.

It‘s actually pretty normal behavior and consistent with how (public) class variables work in most languages IMO.

Kind regards,

This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).