LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic registering of events using an array of refnums?

I'm trying to register multiple events for use in a subVI to handle user input for my application. I've read up a bunch on dynamic event registration. I know it is possible to place the refnums into a cluster for use with the 'event source' terminal of 'Register For Events'. I was wondering if it was possible to use an array of refnums instead. That way I don't have to edit the front panel and terminals of my subVI every time I decide to pass it an additional refnum / event.

 

Currently if I wire in an array of refnums to 'Register For Events' I end up with only one entry under 'Dynamic' event sources (whereas if I use a cluster, there will be many). Is this because LabVIEW won't know what the array contains until runtime and therefore it wouldn't be possible to know before runtime what event sources exist?

 

Thanks in advance for any thoughts / opinions / musings on this.

0 Kudos
Message 1 of 9
(4,047 Views)

I haven't tried this but I would be very surprised if it worked. The problem is that an array has to be an array of exactly the same things, and while the items in questions are all event registrations, they are (at least potentially) all different with different data, etc...

 

Mike...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 2 of 9
(4,040 Views)

With an array of renums, it treats it all like a single control.  Think of it like when you statically create an event case for multiple buttons.  It has to do this since 1) all of the reference types must be the same and 2) it is unknown how many renums will be passed in the array.  With clusters, everything is fully defined.


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 9
(4,033 Views)

@regenerator wrote:

 

Currently if I wire in an array of refnums to 'Register For Events' I end up with only one entry under 'Dynamic' event sources (whereas if I use a cluster, there will be many). Is this because LabVIEW won't know what the array contains until runtime and therefore it wouldn't be possible to know before runtime what event sources exist?


Yes.

 

The obvious thing is to also just try running it and seeing how it behaves. You should find that it does work and that the single event case does handle the events from all of the controls referenced in the array.

 

If you want to have multiple strictly type cases but not to edit things all the time, you can make the cluster of references a typedef (which you should generally do with clusters used in more than one place anyway). That way you just add the reference to the new control to the typedef and it's updated everywhere. You will have the code than bundles the cluster and the code for handling the event, but you would have to do that anyway.


___________________
Try to take over the world!
0 Kudos
Message 4 of 9
(4,026 Views)

Thanks for the responses. I hadn't thought about using type defs to make the subVI wiring more straightforward.

 

It makes sense that you can't pass an array of refnums to 'Register For Events' and then configure more than one event case because you can't know how many elements will be in the array at runtime.

 

However, I gave it a shot and wired up a simple example:

 

RegisterEventsWithArrayOfRefnums.png

 

There is only one dynamic event case that can be configured, but changing either boolean results in an event occurring and the 'CtrlRef' corresponds to the changed control. I guess another way to think about it would be that wiring an array of refnums will cause an event to be generated if any of the refnums undergo a Value Change. This is basically what crossrulz said above (thanks :)). What I was unsure about was what exactly 'CtlRef' would point to, or if you'd be able to determine which refnum the value change occurred in.

 

Does anyone know if this behavior is documented somewhere? It seems like event registration is a bit lacking in terms of official documentation.

0 Kudos
Message 5 of 9
(3,996 Views)
Your logic looks good. This basic technique can be very helpful when you have a number of buttons that do similar things. You can have one event that handles all the value change events and then use the label to see what button was clicked.

The control reference you get from the event data can be used in any way you would a reference from the control itself. For example is a button toggles between two states, you can use this reference to change the button's boolean text to reflect its state change.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 6 of 9
(3,989 Views)

You can also use arrays of references of different types; booleans and strings and numerics, etc.   The OldVal and NewVal will switch to Variant.  As an added advantage, the data in the Variant will be named the same as the control, so you can get the name via "Get Type Information.vi" which is much quicker than a property node.  

Message 7 of 9
(3,971 Views)

I'm currently using an array of refnums to different types, also dealing with variants. I didn't know about the 'Get Type Information.vi' trick though to extract the name. Thanks!

0 Kudos
Message 8 of 9
(3,955 Views)

The Get Type Ino is relatively new.

 

 

IN the interest of ease of support I will use the cluster approach. This makes it easy to see "at a glance" which events aaare registered by the sub-VI and then locate the event case that handles that event.

 

Just my 2 cents,

 

Ben

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 9 of 9
(3,884 Views)