05-10-2017 07:44 AM
This question have been asked before, but I know but I don’t understand the answers.
Therefore, I have to ask again.
Is there a way to convert string array to enum at runtime.
I have to use a ring control, I think.
But how?
Solved! Go to Solution.
05-10-2017 07:48 AM
Post a link to one or a few of those threads you don't understand.
Explain in more detail what exactly you are trying to do.
Did you see this message thread on the idea exchange?
http://forums.ni.com/t5/LabVIEW-Idea-Exchange/convert-String-Array-to-Enum/idi-p/1025934
05-10-2017 07:55 AM
How are you planning to use Enum when you want to update the items during runtime? For what purpose are you trying to use Enum?
05-10-2017 07:58 AM
If you already have an Enum defined, then Scan from String will do this for you. Here's an example -- I have a State Machine whose States are saved as an Enum. At one point in my program, I transmit the State, encoded as a String (I use Format into String to do this, of course), to a routine that needs to take action based on the Enum.
I put my Enum string as the String Input, a Constant representing the Enum itself in the "Default" input, and the output (shown not wired in this Snippet) is the resulting Enum value represented by the String. Note that Scan from String will return Error 85 if the Enum string does not "match" one of the Enum choices -- you can "trap" this error to handle the "Not-an-Enum-Value" situation.
Bob Schor
05-10-2017 08:00 AM - edited 05-10-2017 08:02 AM
This can be done using the Scan from String function:
You will need to have a "default" value in your enum to handle any index in the string array that does not match with a corresponding value in the enum.
Edit: Bob beat me to it by 2 minutes!
05-10-2017 08:14 AM
@Michael.Koppelgaard wrote:
Is there a way to convert string array to enum at runtime.
I have to use a ring control, I think.
But how?
1. An enum has to be defined at edit time.
2. A ring can be edited at runtime. You just use a Property Node to edit the Strings property (which is an array of strings). Just note that your block diagram will only be able to use the index numbers, not the strings you put into the ring.
05-10-2017 01:18 PM
Thank you for your replies.
The reason for wanting an enum is as follows:
I open an excelbook and read the sheet names into a string array.
I want to let the user select which sheet he/she want to look at, by selecting it from an enum.
I don’t if enum is the best control to use. Other suggestions are welcome
I tried you code serwin but I get an error "Error 85 occurred at Scan From String (arg 1) in Untitled 3"
05-10-2017 01:21 PM - edited 05-10-2017 01:36 PM
Error 85 occurs when the input string does not match a value in the enum. You must have a corresponding value in the enum for every input string.
The Scan from String terminal "default 1" that the enum is wired to defines the enum output if it does not find a match in the input string.
Try this to handle the error that is generated:
05-10-2017 01:50 PM
Fine but the output is an array and not an enum?
05-10-2017 01:54 PM
I'm confused. Do you want the user to select an enum value, that will be formatted into a string to select an excel sheet?