356 views
in Embedded Wizard Studio by
Hi.

I want to know how to convert from Enum type to String type.

 

example.

Enum type "Fruit", Items "Apple", "Banana", "Melon"...

=> Fruit.Apple -> "Apple"

1 Answer

0 votes
by
 
Best answer

Hello,

Such conversion is not directly supported. You will need to implement it as a switch-case statement handling in it all the possible enum-values and convert them individually to strings. For example:

var SomeUnit::Fruit enum_value = ...
var string          enum_str   = "";

switch ( enum_value )
{
  case SomeUnit::Fruit.Apple  : enum_str = "Apple";
  case SomeUnit::Fruit.Banana : enum_str = "Banana";
  ...
}

trace enum_str;

In the case the enum items are numbered consecutively, you can convert the enum value into an integer and use the integer as selector to select a string from an array of strings. For example by using a locally defined array:

array string fruits[4];

fruits[0] = "Apple";
fruits[1] = "Banana";
...

var SomeUnit::Fruit enum_value = ...
var string          enum_str;

enum_str = fruits[(int32)enum_value ];

For optimization purpose, you can manage the array as a member existing within a class. See array member.

I hope it helps you further.

Best regards

Paul Banach

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...