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
JackDunaway
Trusted Enthusiast

 

It's currently possible, but there are downsides to each method: 

 

  1. Property nodes are SLOW and don't play well with RT. Plus, you must introduce a potentially unnecessary FP control.
  2. Plop a constant on the BD and select the last item. It's value+1 should be the number of items, ASSUMING 0-indexed  and not sparse (I hope we get sparse enums, which would render this method useless). Fragile, since you must update this code if you add a new last item.
  3. Is robust against adding items, but not robust against changing datatype. Also, sparse enum would break this method.
  4. Fun for you. Most robust method I know, but quite frankly, a pain.
  5. Not shown, OpenG "Enum to Strings". Downside: not included in LabVIEW base package.

 

 

CountEnums.png 

 

 

Dragis
Active Participant

you forgot one of my favorites 😉

 

enum-size.png 

JackDunaway
Trusted Enthusiast
Clever! Unfortunately, that one is also destined for failure for non-sparse enums.
JackDunaway
Trusted Enthusiast
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.
Intaris
Proven Zealot

How about this then?

 

Enum Data.png

Intaris
Proven Zealot

Actually, there's a small bug in that one.  Since the name position is padded to lie on a 16-bit boundary, following code will perform better.

 

On an aside, I spent a lot of time decyphering the Type descriptors years back..... 

 

Enum Data_2.png

JackDunaway
Trusted Enthusiast

Intaris, thanks for the illustration of what the "Black Magic" VI would look like in my #4 - I wasn't on a computer where I could dig up a type descriptor decoding VI. By the way... inventive enum items... Smiley Wink

 

The spirit of this idea remains that a simple task would be better accomplished with a simple primitive rather than a somewhat sophisticated understanding of LabVIEW data storage.

 

 

 

Message Edited by JackDunaway on 04-01-2010 01:56 PM
Intaris
Proven Zealot

Jack,

 

I agree it would be nice to have something to do this.  And add to it a function to return an array of valid enum values while it's at it (output as enum, not string).

Message Edited by Intaris on 04-01-2010 02:42 PM
Manzolli
Active Participant

There is a very simple method to do it, the property "Number of Items". The only downside of this method is speed.

 

Enum Property: Number of Items.jpg

André Manzolli

Mechanical Engineer
Certified LabVIEW Developer - CLD
LabVIEW Champion
Curitiba - PR - Brazil
JackDunaway
Trusted Enthusiast

Ha, forgot the most obvious one. Well, it suffers the same downsides of #1 - try using that in RT. Never read the context help on that one... interesting choice of a "useful" application... (maybe it refers to a Pict Ring?)...

 

***EDIT*** Also, subroutine execution priorities cannot contain a PN.

 

 

Message Edited by JackDunaway on 04-01-2010 03:02 PM