In addition to the global variable types, sequences have two variable types of their own. They are Private and Static. Where Global and Registry variables exist throughout DAQFactory and are useable in any expression or local sequence, Private and Static variables only exist within the sequence in which they were created. This makes it so you can use the same variable names to refer to different data in two different sequences. In general, if you are creating a variable in a sequence and you only plan on using within the sequence itself you should use a Private or Static variable.
Private variables are declared using the private statement.
private X = 1
private string MyString = "string"
private Y = {1,2,3}
Like regular global variables, you can assign arrays or strings to private variables. Also like global variables, you can set the private's initial value in the declaration statement.
private X = 3
while (1)
X++
delay(1)
endwhile
So in the above sequence, X will be assigned to 3 and then incremented from there every second within the loop. The only difference between a global and Private is that the Private can only be seen by the sequence and once the sequence ends, the data in the Private is lost. Because of this, the private must be redeclared the next time the sequence is run. So, looking at the above sequence again, if the sequence is stopped and then restarted, X will be cleared when it is stopped, and will be redeclared and set to 3 again when restarted.
Privates are most commonly used for loops:
Private X = 0
while (X < 10)
Output = X * 5
X++
endwhile
This sequence simply sets Output to 0 through 45 in steps of 5.
Statics are almost the same as Privates, but unlike privates, they do not lose their data when the sequence ends. Instead, when the sequence is run again, the Static will still have the value it was last set to. This type of variable has its particular uses, but is not very common. Declaring statics is just like private and global variables:
Static Y = 0
Static String MyCompany = "AzeoTech"
Unlike other variable declarations like private and global, the assignment is only made if Y does not already exist. So if this command is in a sequence and you call the sequence twice, the second time through it will not assign 0 to Y, but will simply ignore the statement as Y was declared the first time through. This allows you to initialize the static.
Remember that since statics persist even when a sequence stops and restarts that the above lines will only set the variables to the indicated values once. So:
Static Y = 0
Y++
? Y
will print 1 to the command window the first time it is run, and then 2 the next time, and so forth because Y only gets initialized to 0 when it is declared and doesn't exist already. If you used a private, 1 would be printed each time because the variable would fall out of scope and be reinitialized the next time the sequence is run.
Note: Although Statics retain their data after a sequence stops, they do not retain their value when DAQFactory quits. For this you must use a Registry variable.