Enumerations and strings

Enumerations can be very useful in Mendix. For example they are the only type other than a boolean, that can be used to Split your microflow. Also, each value can an image, which you can show in your data grids.

Sometimes it is necessary to convert an enumeration value to a string and vice versa. For example when using SOAP webservices where you do not want (or are not allowed to) use Enums.

In this blog post I show how to convert between Enum-values and strings, and explain why this is best way to do it. I will use the following enumeration as an example:

Creating a microflow to convert an enum value to a string is easy:

Unfortunately, the other way around is not so straightforward. This is method A:

Suppose the product owner decides that the functionality needs 8 possible directions instead of 4. We can easily add them to the enumeration: NE, SE, SW, NW. 

But if we used method A, you can clearly see  a drawback in maintainability: The conversion microflow also needs to be extended!

There is a nicer and better way to convert a string to an enum value: Create a Java action with 1 string parameter that returns an value of your enumeration:

Put this in the user code of String_To_Direction.java (after you press F6, you can find this file in the [project]\javasource\mymodule\actions directory):

try { 
  return mymodule.proxies.Direction.valueOf(sourceString).name();
} catch (IllegalArgumentException e) {
  return null;

And call this Java-action in the conversion microflow:

Now you don’t have to worry about the conversion microflows anymore when the enumeration changes during development.

One response to “Enumerations and strings”

  1. BORNIER Avatar

    Nice, but it s a too limitative solution. We should create for each enumeration a microflow because Mendix did not offer the option Any Enum parameter and no function to get the class name of the enumeration (module.enumerationName) or an EnumToString an StringToEnum functions… perhaps in a next version ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.