From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Lifetime of variables

Hi,

 

Can anybody provide me with some guidance on what type of varaibles to use if I am dealing with Events in my script?

 

I wish to interact with a cursor, and can successfully call a function on the cursor event. However, say I wish to count the number of times this function has been called, where is the best place to declare this variable and what type should it be?

 

So I could use: Dim iEventCount    at the top of my .VBS file

 

or I could use Call GlobalDim("iEventCount").

 

The first one does not seem to work, it does not retain the value of the variable between events. Is this expected? 

0 Kudos
Message 1 of 5
(4,188 Views)

Hi Neil,

 

The variables declared with Dim in a regular VBScript terminate when the VBScript ends, so it's expected behavior that those variables don't persist.  The GlobalDim() function creates a variable that lives in DIAdem application memory, so those variables will persist until DIAdem ends.  If you call GlobalDim() a second time for that same variable, though, it will initialize that variable back to a count of 0, so you need to GlobalDim() that variable only once.

 

My recommendation would be to declare your global variables in the same VBScript that adds your UserCommand and defines the function name to be called for your VIEW event.  Ideally that VBScript will run only once on DIAdem application start.  Let me know if you want tips on setting up DIAdem to run scripts automatically each time it starts.

 

Another design option to consider would be to attach this count as a property to the Root, or to a Group, or to a Channel in the Data Portal.  That won't make sense for all scenarios, but there are cases where the information you're tracking will pertain logically to one or more entities in the Data Portal.  In that case, it might be advantageous for that value to save and load with the data set.  Also, if you wanted to keep track of how many times the code was run per Group or per Channel, you could easily use the property approach to keep those counts separate, by always incrementing the property value attached to the Group or Channel which just got run again.  This approach is also easier to integrate in REPORT layouts, in case that's interesting to you.

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 2 of 5
(4,180 Views)

Thanks Brad,

 

Actually after a bit more investigation it does seem that normal "Dim" variables do actually keep their value when dealing with Events.

 

I have my code working quite nicely now, as the user scrolls the cursor I can dynamically load in different video files. My next problem is that I sort of hoped the "Play" button in the VIEW would result in the same behaviour, but it seems that although the cursor does move automatically when I press play I do not get any of my Cursor change events. I really don't know how to deal with this.

0 Kudos
Message 3 of 5
(4,123 Views)

Hi Neil,

 

It actually makes a difference which VIEW Area you last clicked on before hitting the "Play" button.  Please double-check that you leave the data graph as the active VIEW Area before hitting the "Play" button.  If that doesn't help, please trying setting the playback speed much slower, say to 0.1.

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

Message 4 of 5
(4,039 Views)

Ah, thanks Brad, I have just seen this post now.

 

It did just magically start to work for me, so I think perhaps I was subconciously doing what you have just recommended. It is good to know the limitations.

0 Kudos
Message 5 of 5
(3,855 Views)