05-24-2010 12:04 PM
Today's nugget is a quick one. It's been mentioned several times in the forums before, but I was surprised to discover today that I hadn't actually written a nugget about it. So here goes...check out the Format Into String function in the code below:
This code is used to construct a table of different sections of code and the amount of time each section took to execute. The "Code Sections" array contains the enum values that govern the execution of my state machine. As you can see, I'm using the Format Into String function to turn each enum value into a string. Format Into String can also turn other data types (paths and Booleans come to mind) into strings, but I have found this enum trick to be the most useful, since there seem to be many times that I need to turn an enum value into its corresponding string value for one reason or another.
05-24-2010 04:14 PM - edited 05-24-2010 04:15 PM
That's a very useful technique. Thanks for sharing it Darren.
You can reverse that operation with the Scan From String function. All you need to do is wire the enum type into the element and out the other side comes the proper enum value. If the value in the string does not match a string value or numeric value in the enum then the output will default to the value of the wired in element.
I find this very useful when working with state machines and the like that receive commands and/or data from a remote target via TCP/IP, serial or other. Presently I am using the technique in an application running on an sbRIO that is being controlled by a serially interface touchscreen. It works great.
Jason
05-24-2010 04:54 PM
05-25-2010 02:08 AM - edited 05-25-2010 02:16 AM
Something which surely can't be left out of this nugget is the fact that the reverse also functions.
Following code runs flawlessly. Of course, if the string does not exactly match one fo the enum strings, the conversion will fail. But sometimes, this is OK.
Shane
PS it also works if you wire an enum constant to the second function.... Just in case somebody thinks it's always returning the default value. I use this function in some of my INI saving / Loading code.
05-25-2010 05:12 AM
For some reason I completely missed that Jason had already mentioned this....
Sorry Jason. Kudos.
Shane.
05-25-2010 07:40 AM
Intaris wrote:For some reason I completely missed that Jason had already mentioned this....
Sorry Jason. Kudos.
Shane.
No problem Shane. At least you thought to include example code. And as LabVIEW has taught us, a picture is worth a thousand words.
Jason
05-25-2010 07:59 AM
05-25-2010 08:20 AM
05-25-2010 10:55 AM
PaulG. wrote:
Format into string is one of the more useful primitives and I use it often. But I didn't know you could use it in reverse. That's cool.
Just to clarify, when going in the reverse direction, the primitive being used in the snippet above is Scan From String.
05-25-2010 12:39 PM
When you use "Format Into String" with "format string" unwired and an enum as an input, the format string defaults to "%s". You can explicitly define the format string to interpret enums as either the element string or the element index: