LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
falkpl

For loop accepts enums as enums

Status: New

I often employ a design pattern of for each item in a list where the list is a typdef enumerated type.  This is a great way of making a linear sequence where each item is tested one time (test sequences, list checking .....).  I used to make an array of the type and populate it with one of each item, this is not very scalable since when I change the typdef I have to redo the list.  Instead I do a little type manupulation to make sure that I itterate one time for each intem in my enum, this requires some support code and extra wiring.  It would be nice if I could make a for loop that takes the enyumerated type and used it as its itterator.  See below:

 

 

 

 

For each enum loop.jpg

The new for loop which takes an enum would greatly simplify the code readability.

I use this all the time and find it to work great, uppdate my enum and handle the new case, no chance of missing a case (I use it in conjunction with case structures with no defaults so my code breaks and forces me to handle the new case.

 

I have attached the code if any one is interested

 

 

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
9 Comments
Intaris
Proven Zealot

Hmm. A "For each X in Y" option.

 

At first I thought no, but I'm not so sure any more.  It could be interesting as long as the terminaly are smaller.... 😄

Jarrod_S.
Active Participant

There's a VI that can read the information about an enum. It lists all the strings in an enum without using a Property Node. The VI is called Get Numeric Info.vi, and is located in <vi.lib>\utility\VariantDataType. You can use this VI in combination with the Scan from String function as an alternate way to scan through all the items in an enum.

 

Enum.PNG

Jarrod S.
National Instruments
JackDunaway
Trusted Enthusiast

Here's another example for looping through enums. It has lower overhead than the previous two examples, but has the disadvantage that you must correctly type the "railed out" U16 (it must match the representation of the enum). One other stipulation: it assumes the enum is consecutive and starts at 0 (the current limitation of an enum, although we hope this restriction will be removed!, at which point Jarrod's more robust method wins). I have used this method as Subroutine priority on RT.

 

(As an aside, I really like Jarrod's VI... getting the name without a PN - that's fancy. I have always used a PN Value Write followed by a RingText.Text on a dummy enum.)

 

LowOverheadEnumLooping.png

Message Edited by mechelecengr on 08-24-2009 04:00 PM
Message Edited by mechelecengr on 08-24-2009 04:06 PM
TCPlomp
Trusted Enthusiast

I don't think this is intuitive.

An enum represents a value, so by sending 'Monday' I would run the for loop once (defining sunday as day 0), tuesday run twice. The names and the value are both important to an enum so which one should be chosen is ambigious.

 

Ton

 

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
falkpl
Trusted Enthusiast

Maybe the 'Monday' is a little ambigious (since we are just concerned with the data type and not the value of the constant) but the N terminal is not the number of itterations but itterates for each element in the enumerated type.  This is useful if you have a part number or something with multiple unique vqlues that you want to itterate FOE EACH element.  When the list changes so does the code.  The N icon probably should change.  I have several vis that I have made to convert enums (using variant data types) to get the number of elements, and create a string list of the elements.  I was looking for a cleaner solution.  I dont think the I and N terminal would have identical meaning as the traditional for loop.

 

 

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Chris_in_Colorado
Member

What works a ton better is a text list, since enums are so picky.

 

I've writte a VI for myself, an 'engine".

 

 

RavensFan
Knight of NI
Maybe instead of adding options to the For Loop to do something like this, it should be basically a new structure called a For Each Loop.  (Related but it's own structure).  Instead of having an N terminal, it could have a FE terminal (for each) which is essentially a variant that could take an enum, array, ring, ....
TCPlomp
Trusted Enthusiast

I disagree, A 'For Each' loop is exactly what a For Loop with autoindexing is.

using an enum is just dumb IMHO for this kind of action, and as jarrod has shown it is quite easy to pull off.

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
falkpl
Trusted Enthusiast

I completly understand the use of auto indexing and have manipulated enums via the variant type, but would not go as far as calling this idea dumb.  I would like a method for taking existing lists that are of enum type and converting them to be easily adapted to use with the for loop.  I find an enum very nice to use and would like to itterate over it just as I do with an array.  An alternate solution would be similar to Jarod's would be a vi that takes a enum and returns an array of enums instead of strings but this can only be done with scripting as far as I know since the output would have to adapt to the input enum type.  I dont agree that an array of strings is the same exactly as an array of enums since the enum will adapt to the case structure to enforce coding of unique cases, but strungs you have to type in each value and keep track of the changes in spelling.

 

 

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA