LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert string array to enum at runtime

Solved!
Go to solution

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?

 

0 Kudos
Message 1 of 24
(11,916 Views)

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

0 Kudos
Message 2 of 24
(11,905 Views)

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?

-----

The best solution is the one you find it by yourself
0 Kudos
Message 3 of 24
(11,900 Views)

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.

Scan from String.png

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

0 Kudos
Message 4 of 24
(11,896 Views)

This can be done using the Scan from String function:

Capture.PNG

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! Smiley LOL

Message 5 of 24
(11,894 Views)

@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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 24
(11,885 Views)

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

 

snip.png

 

I tried you code serwin but I get an error "Error 85 occurred at Scan From String (arg 1) in Untitled 3"

0 Kudos
Message 7 of 24
(11,850 Views)

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:

VI snippet.png

0 Kudos
Message 8 of 24
(11,846 Views)

Fine but the output is an array and not an enum?

0 Kudos
Message 9 of 24
(11,840 Views)

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?

0 Kudos
Message 10 of 24
(11,835 Views)