04-27-2010 07:31 AM
Hi!
I have basic question:
1. What is the difference between the use of local variable and using property node (provided it is linked to an appropriate variable)?
2. What exactly is global variable and local variable?
I try to read its definition.. But I could not get the meaning.
Thank you.
04-27-2010 07:37 AM
Use global variable if you need to access a variable from different VI's/Sub-VI's.
Local Variables you can access them only from the VI you have created them.
Hope that helps...
Dimitrios
04-27-2010 07:43 AM
If you can use local variable or property node is better to use local variable because is faster.
But if you can avoid both of them by connecting wires that's much better practice.
Dimitrios
04-27-2010 07:48 AM
Have a look at that explanation also
http://digital.ni.com/public.nsf/allkb/74ECB57D3C6DF2CE86256BE30074EC47
04-27-2010 07:50 AM
Thanks Dimitrios!
I will read the link you gave me,
about global variable, I am still a bit confuse..
04-27-2010 07:56 AM
There is possibility that local variables creating race conditions
Why some people say Local Variables are bad.
04-27-2010 08:08 AM
Ok, lets say you have 2 different independent programs saved as Untitled1.vi and Untitled2.vi.
When run Untitled 1 is doing some calculations. At the same time you run in parrallel Untitled 2 that does some other jobs but in order to do them it needs to know the result of those calculations. Then you would write the results of those calculations to a Global variable on Untitled1 and you will read the same global variable on Untitled 2.
If you can use only one VI (let's call that Untitled3.vi) that does both the calculations from Untitled1.vi and the jobs from Untitled2.vi (using a flat-sequence for example)then you may pass the results of the calculations to a local variable (if that's the best way - not always).
Then you can read that local variable on the same block diagram to do the other jobs you want.
Not the best example but difficult to put it into words.
04-27-2010 08:13 AM
First of all, the equivalent of variables in traditional languages are wires. So whenever possible pass the data via wires!
Local variables are useful if you need to programatically inialize a control. Property nodes (value) allow to do the same inside a SubVi via a control reference, but they are much slower.
Globals are fast and can be used for look-up tables or 'global settings'. You should make sure they are initialized at the beginning of your program and never written again (Single writer).
Globals ans Locals can be misused as means to communicate between parallel tasks, but using queues or LV2 globals is a far better approach.
Felix
04-27-2010 08:42 AM
The code described by Dimitrios is a perfect example of a bad usage of local variables. All this can be done by using a wire. And this also eliminates the need for a sequence, because the execution is determined by dataflow.
Felix
04-27-2010 08:45 AM
Here is a Nugget on Action Engines (LV2 globals) that discusses the issues with race conditions and how they can be avoided.
Ben