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

I'm pretty new to LabVIEW and I'm using global variables in a VI for the first time. I've run into a problem where if I create a new global variable, clicking on it does not bring up a list of front panel objects from my global panel. No list appears. If I double click, I am brought to a new second global panel.

 

How do I get a new global variable vi to refer to the first global panel?

 

I can post the VI if needed but I'm not sure how to show this issue without posting the whole project, which is a bit large. If that's the thing to do just let me know, or if there's some other way to do it.

0 Kudos
Message 1 of 16
(1,711 Views)

Well, in general best coding practices, you shouldn't be using global variables at all.

 

That said, if you do insist on it, open the panel for the first global variable that already exists, and on that front panel, put down a control there that matches the data type and name of what you want the second global to be, and now you've stored two global values in one global VI.

0 Kudos
Message 2 of 16
(1,706 Views)

I just drag the global variable file from the project window.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 16
(1,699 Views)

@Tarnarmour wrote:

I'm pretty new to LabVIEW and I'm using global variables in a VI for the first time. I've run into a problem where if I create a new global variable, clicking on it does not bring up a list of front panel objects from my global panel. No list appears. If I double click, I am brought to a new second global panel.

 

How do I get a new global variable vi to refer to the first global panel?

 

I can post the VI if needed but I'm not sure how to show this issue without posting the whole project, which is a bit large. If that's the thing to do just let me know, or if there's some other way to do it.


If you are new to LabVIEW then you should definitely be avoiding global variables.

0 Kudos
Message 4 of 16
(1,692 Views)

I know how to add controls and stuff to the global front panel, my problem is that when I place a global variable function into my VI I can't access any of the controls on that global front panel. Maybe I'm misunderstanding something but from the tutorials and examples I've looked at it seems that you use one global front panel to store variables you need to share among different VI's, then each global variable function block can be set to either read from or write to one of the front panel elements on that global panel. When I place a new global variable function block, it creates a new global front panel which is completely empty. I need it to connect to the populated global front panel.

 

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.

0 Kudos
Message 5 of 16
(1,674 Views)

I'm not sure what you're saying. Do you mean post just the global variable front panel?

0 Kudos
Message 6 of 16
(1,672 Views)

@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).

0 Kudos
Message 7 of 16
(1,654 Views)

@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).


NI Week 2016: Are Global Variables Truly Evil?

 

I somewhat regularly use Global Variables as "constants".  I set them to private inside of a library or class to avoid having outside forces changing them.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 8 of 16
(1,646 Views)
Solution
Accepted by Tarnarmour

Three ways:

  1. If your global VI is open, drag its icon and drop on a block diagram.
  2. Drag the global VI from Windows Explorer onto a block diagram.
  3. Right-click on a block diagram, then select: Select a VI ... and browse to the global VI.
"If you weren't supposed to push it, it wouldn't be a button."
Message 9 of 16
(1,645 Views)

@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.


0 Kudos
Message 10 of 16
(1,643 Views)