From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

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.

20 Comments
manu.NET
Active Participant

Hello,

 

In the same way ... I would also need something like ... "For all items of an enum".

 

I know that this could actually be made by using a property of an enum control reference ...

 

But it would suppress the need to put a "stub enum control" on a front panel ... only to get all its values !

 

Manu.

 

 

Manu.net
nathand
Proven Zealot

Found one more "Variant" method that ships with LabVIEW thanks to this post by nathand found in "...\vi.lib\utility\VariantDataType\GetNumericInfo.vi". I'm guessing it's an encapsulated version of my #4. But does it play well with RT? Don't know.

I haven't seen any problems using functions from the VariantDataType library on RT.

Manzolli
Active Participant
I could not figure out what kind of animation can be done with a non graphic read only property!
André Manzolli

Mechanical Engineer
Certified LabVIEW Developer - CLD
LabVIEW Champion
Curitiba - PR - Brazil
James_W
Active Participant

num of emum items.PNG

 

Well I don't use RT, but I'm sure this is RT compliant, the problem is that compared to a property node it is probably much slower!!

Advantage of this method -

1) no property node,

2) doesn't matter which element of the enum you start from,

3) copes with enum updates easily.

4) Depending on the way they are implemented, I would expect this to cope with and handle sparse enums.

 

On a tiny enum this may beat Manzolis method, but add loads of elements, and I'll leave someone else to do the analysis.

Message Edited by James W on 04-14-2010 02:42 PM
CLD; LabVIEW since 8.0, Currently have LabVIEW 2015 SP1, 2018SP1 & 2020 installed
shew82
Member

There's a really nice simple way to do this that (a) doesnt use property nodes, (b) doesnt need any OpenG Black Magic...

 

...it uses a little bit of NI Black Magic in the form of a VI in vi.lib:  vi.lib\Utility\VariantDataType\GetNumericInfo.vi 

 

This will give you a list of strings in an enum which in turn tells you how many elements it contains:

Num of Enum Items (no UI).png 

Neil.Pate
Active Participant

I am late to the party here, but +1 for shew82's solution.

 

DavidBoyd
Active Participant

Well, I blame Neil for bringing an old topic back to the forefront. Smiley Very Happy

 

Of all the answers I've seen, I like mine the most, so I'll add it here.  Doesn't need controls, property nodes that cause a FP context hit, make arrays of strings, etc.

 

 Get enum element count.png

David Boyd
Sr. Test Engineer
Abbott Labs
(lapsed) Certified LabVIEW Developer
rbrown_acertaralabs
Member

An alternative and straightforward way to do this is to simply add an "END_OF_ENUM" item to your enum.  "END_OF_ENUM" is simply my choice of label...you can name it whatever you like.  It's direct and simple and you'll have to be aware within your app that you have that final item, but that is a small price to pay for the benefit of having this end item (no casting tricks and no performance hits from front panel property nodes).  When you update the typedef enum, just add new items before the END_OF_ENUM item, but that should be obvious and self-documenting.  Here's how it then looks to drive a for loop:

 

001554.png

 

wiebe@CARYA
Knight of NI

I think there should be a min\max value function that returns the minimum value and maximum value of anything wired to it. For instance, an I8 would return -128 and 127, an U8 0 and 255.

 

This would be useful for vim development, and would also be useful for enums. The return type would still be the input type, so a To Long Integer and an Increment would still be needed for a for loop...

wiebe@CARYA
Knight of NI

FYI, LV2020 has a function for getting the min\max value for a type (Range Limits for Type.vim).

 

Sadly, for enums, we still need to convert to I32 and add 1 to use it in a for loop.

 

It would pretty easy to make another .vim that does this.