Jump to content

LabVIEW Equivalent of Visual Basic's 'For Each' Syntax


Recommended Posts

I'm wondering if anyone knows a better solution to this...

If I call a (for instance) Visual Basic DLL, and it returns a reference to a VB Collection (since LabVIEW doesn't directly support Collections), what is the best way to perform the equivalent of VB's 'For Each' on this Collection in LabVIEW?

If the Count of the items in the Collection is known, you can wire that to a For Loop in LabVIEW. However, if this Collection is an arbitrary length, the only option I know of is to use a While Loop and attempt to index an Item each iteration. Then I wire the Error Out of the property node to the loop control terminal to tell it to stop when we receive an error (which should mean we are out-of-range on our index).

This method seems inefficient and just plain wrong. But I don't see any other way to do this. What do you all think?

(Sorry if few of you have experience with Visual Basic...)

-Ian

Link to comment

I'm wondering if anyone knows a better solution to this...

If I call a (for instance) Visual Basic DLL, and it returns a reference to a VB Collection (since LabVIEW doesn't directly support Collections), what is the best way to perform the equivalent of VB's 'For Each' on this Collection in LabVIEW?

If the Count of the items in the Collection is known, you can wire that to a For Loop in LabVIEW. However, if this Collection is an arbitrary length, the only option I know of is to use a While Loop and attempt to index an Item each iteration. Then I wire the Error Out of the property node to the loop control terminal to tell it to stop when we receive an error (which should mean we are out-of-range on our index).

This method seems inefficient and just plain wrong. But I don't see any other way to do this. What do you all think?

(Sorry if few of you have experience with Visual Basic...)

-Ian

Well. Getting the length of an array and wiring that to the "N" terminal (I think the LV equivelent would be an array of clusters or objects) isn't that inefficient. But labview arrays are "self indexing". If you wire an array through a for loop and don't wire anything to the "N" terminal, it will iterate for the number of elements that are in it.

Edited by ShaunR
Link to comment

I'm not a VB developer, but my experience both with the Office API and with various .NET APIs has been that they do have a Count property. Are you saying this isn't a built-in part of collections? I would expect there to be some standard method of retrieving its size.

Link to comment

I'm not a VB developer, but my experience both with the Office API and with various .NET APIs has been that they do have a Count property. Are you saying this isn't a built-in part of collections? I would expect there to be some standard method of retrieving its size.

I see what you are saying here. Most collections I access in VB do have a Count property.

I worked on a project that communicates with Fanuc robots, and used their PC Development Kit, which is a set of ActiveX objects. When the robot has an error, it shows up in a collection of Alarms. The problem with this Alarms collection is that the Count always seems to return 100, whether there are actually 100 alarms or not. Iterating over this using the Count means that you have to look through all 100 alarm obects, even though only one or two may have actual alarms.

I guess that the obvious solution for that problem is to stop when the Alarm object has a non-alarm (or empty, I can't remember which). As I'm writing this, I'm thinking that this may only be an issue with this particular set of objects, and most Count properties would work as expected.

So I will settle on the idea that the Count property should be wired to the For Loop's N terminal to get the proper number of iterations over the collection. I will have to try this out.

Thanks,

-Ian

Link to comment

You should note that the for loop has an optional stop terminal (made available with a right click) which is basically similar to the Break command (although unlike Break it will always finish the code in the current iteration). You can use this to stop a for loop prematurely in the cases where the Count property doesn't behave.

  • Like 1
Link to comment

I worked on a project that communicates with Fanuc robots, and used their PC Development Kit, which is a set of ActiveX objects. When the robot has an error, it shows up in a collection of Alarms. The problem with this Alarms collection is that the Count always seems to return 100, whether there are actually 100 alarms or not. Iterating over this using the Count means that you have to look through all 100 alarm obects, even though only one or two may have actual alarms.

I recently took over responsibility for our Fanuc robot, though I have not had a chance to really dig into it yet. I'm interested in what you come up with.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.