LabVIEW Idea Exchange

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

enumerated typecast vi

Status: Declined

Any idea that has received less than 3 kudos within 3 years after posting will be automatically declined.

Type casting from a number to an enum will use only the first (highest) n bytes that it needs to do the representation.  In most cases I would argue this is contrary to what most people would expect to occur.  To remedy this case, a casting needs to be done on the numeric input before hand so that for example, the number 1 goes to the enumerated value with a number 1. 

 

My suggestion is to have a vi that is specifically designed for typecasting to an enumerated value.  This vi would be free to cast the numeric input, so as to match the enumerated input.  It could also take in an input string, and try to match the string to the strings values of the enumerated type.  I could implement this myself, and might, however it seemed like it might be useful to have that in the same panel as the typecast so that one would be more inclined to use it than the typecast, for enumerated values, and subsequently avoid the problems that come with this unexpected behavior. 

4 Comments
JackDunaway
Trusted Enthusiast

I have never had probelms typecasting from an enum to numeric or vice versa... can you describe what you're talking about in a picture?

 

Are you trying to cast from an unsigned enum to an signed or floating point numeric? Remember, use the type convert functions to get the representation the same, then use typecast to get the type right.

Message Edited by mechelecengr on 08-17-2009 02:04 PM
JimboH
Member

Usually it is a conversion from I32 to an unsigned u16 enum.  I know I need to do the conversion first, but I forget a lot, and the resulting behavior is unexpected.

 

I thought with an inclusion of a vi as shown that people would be less likely to make that mistake.  

 

In addition this vi could go from string to enum, which I don't think is possible without having a control or indicator with a property node that specifies the strings of the enum, which would allow you to search for a match (unless format into string can be coaxed into returning an array of strings, or there is an opposite function "format into enum").

 

enumTypeConversion.PNG 

tst
Knight of NI Knight of NI
Knight of NI

Scan From String (which already exists) is the second function you're looking for - it can accept enums as inputs and scans the input string for the relevant value from the enum.

 

As for the first issue, I usually don't have the need to convert a number to an enum (and in the cases I did, I wrote an explicit VI), although I can see where you're coming from. The state diagram toolkit used to have an external node (something which probably won't work in the latest versions, as they were replaced by XNodes) which did exactly what you want by generating code when you wired the enum to it. Also, OpenG has a merge VI which does what you want by dropping a To Variant followed by Variant to Data.


___________________
Try to take over the world!
Darren
Proven Zealot
Status changed to: Declined

Any idea that has received less than 3 kudos within 3 years after posting will be automatically declined.