LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

New Global Variable creates new front panel and can't access old front panel?

Solved!
Go to solution

Thanks! That's solved it.

0 Kudos
Message 11 of 16
(405 Views)

The one use of global variables that I have made so far was to store a constant start time from the millisecond timer so that I could access it (as a read only value) conveniently to get relative time without having extraneous wires all over the place. Is that an appropriate use? I kind of worried when I wrote it that I was falling into a classic blunder.

0 Kudos
Message 12 of 16
(400 Views)

@Tarnarmour wrote:

@johntrich1971 wrote:

@Tarnarmour wrote:

 

Several responses point out that global variables are a bad coding practice. First, I don't really need global variables for this specific code I'm working on, and won't be using them in the finished VI. I just realized I didn't know how they worked and wanted to learn how to implement them. But second, I slightly disagree with the categorical judgement that global variables are bad. They exist for a reason; there are applications that need global variables, which is what they're designed for. I think what is meant when people say they're bad coding practice is that when appropriate shift registers or other solutions would work better and prevent errors from occuring.


So as a newbie you believe that you know more than people who have been coding in LabVIEW for more than 20 years? When I was a newbie I wrote bad code - and used a lot of local variables and global variables. I also used stacked sequence structures (Ugh, did I just admit that - 😂). I learned better because I listened to what others were telling me. Yes, there are times when using global variables and local variables are acceptable, but they should be used sparingly because they break the dataflow and create opportunities for race conditions. When newbies begin using local and global variables they often begin to see these as the solution to all of their problems, and they create new problems because of the use of these variables. This is why so many have advised against it. I don't think that I've used a global variable in anything that I have written in years (I have worked on some legacy code that used global variables for things such as retaining configuration settings).


It's a fair point about me being new, but to be honest I think you just agreed with me. Global and local variables do have certain acceptable uses, albeit rarely and carefully. I just think it's counterproductive to just shut people down when they try to use them. If they actually learn how global and local variables work, including the risks, they will understand when they can or can't be used. To new learners, being told not to do something just because it's bad practice is (while often totally true) not a satisfying or helpful answer. And if they just conclude that they can never use global variables some day they'll be stuck again when they have some situation that is best solved with global variables.

 

Those are my opinions, but it's also very true that most of you guys answering questions here on the forum have years or decades more experience than I do. I could definitely be wrong.



I don't believe that anyone here said that you should never use global or local variables. The problem with many new learners is that they often don't take the time to understand when there might be a proper use for local or global variables. To be honest I've been coding with LabVIEW for over 25 years and I still avoid these variables like the plague. It's too easy for them to come back and bite, and there are almost always other ways to accomplish what you want without breaking dataflow. You honestly appear to be more thoughtful than many newbies and would perhaps be more ready to understand the proper uses of these variables.

0 Kudos
Message 13 of 16
(397 Views)

I'm definitely new to LabVIEW but I do have at least a few years of programming experience in other languages. Basically every programming language I've learned has come with a similar injunction about global variables, so maybe that prepped me to be more thoughtful 😁. Anyway thanks for the perspective and advice.

0 Kudos
Message 14 of 16
(393 Views)

@Tarnarmour wrote:

The one use of global variables that I have made so far was to store a constant start time from the millisecond timer so that I could access it (as a read only value) conveniently to get relative time without having extraneous wires all over the place. Is that an appropriate use? I kind of worried when I wrote it that I was falling into a classic blunder.


 

I tend to try to organize my code so that I can do this without extraneous wires running all over the place, but that might be an appropriate use (assuming that you're using some method to insure that the millisecond timer runs before starting and has time to write to the local variable before it is read anywhere else).

0 Kudos
Message 15 of 16
(391 Views)

@johntrich1971 wrote:

@Tarnarmour wrote:

The one use of global variables that I have made so far was to store a constant start time from the millisecond timer so that I could access it (as a read only value) conveniently to get relative time without having extraneous wires all over the place. Is that an appropriate use? I kind of worried when I wrote it that I was falling into a classic blunder.


 

I tend to try to organize my code so that I can do this without extraneous wires running all over the place, but that might be an appropriate use (assuming that you're using some method to insure that the millisecond timer runs before starting and has time to write to the local variable before it is read anywhere else).


I store references in globals.  It's also a great place to store parameters taken from a config file.  Note that these are all "write-once-read-many" applications.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 16 of 16
(378 Views)