836 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 - Archive

Welcome to the Ask Embedded Wizard archive. This community forum served us well for many years, but we've evolved our support approach!

Your resources:

The Embedded Wizard Online Documentation provides comprehensive documentation, tutorials, examples and ready-to-use software packages.

For dedicated assistance, explore our Embedded Wizard Product Support.

You can still browse the valuable discussions from our community history here.

Embedded Wizard Website | Privacy Policy | Imprint

...