LabVIEW Idea Exchange

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

Function to Count Number of Items in Enum

Status: New

A common problem I run into is needing to know how many elements (items) are in an enum - I would like to see a primitive function that outputs this number.

 

Proposed syntax: 

CountEnumPrimitive.png 

 

Check out the Comments Section to see some current implementations and their drawbacks.

22 Comments
Petru_Tarabuta
Active Participant

+1. Recent versions of LabVIEW have added the extremely useful Enum to Array of Enums.vim. This enables writing the following, which is perhaps the most idiomatic and self-documenting solution. Not sure how it compares with the solutions posted above in terms of performance.

 

Petru_Tarabuta_0-1741534869382.png

wiebe@CARYA
Knight of NI

Range Limit for Type.vim will be faster but only in theory.

 

In practice it won't make any difference, as the Enum to Array of Enums and the Array Size will probably be constant folded.

 

Range Limit for Type.vim gives the max value, not the number of items. This is tricky. If you want the number of items in an enum (e.g. to enumerate a for loop), you still need to convert the max to an I32 and add one.

 

If we ever get sparse Enum to Array of Enums will still work. 

wiebeCARYA_0-1741598089311.png

 

It's very easy to make a Number of Items vim now... The original idea predates vims.

 

If I'd make such a vim, it would accept enums, array, maps and sets.

 

I'd personally avoid everything using variant magic (expensive API call, also preventing constant folding) or properties (keeps FP in exes, syncs with UI).

 

If LabVIEW had the proposed function, we could stop talking about it.