Friday 8 August 2008

String to Enumeration

This brief post concerns converting the string form of an enumeration back into the enum class version in C++/CLI.

There are plenty of sites around the internet that tell you how to convert the string form on an enumeration value back into the enum value in C#, but I couldn't find one in C++/CLI. The C# is as follows:

status.State = (State)Enum.Parse(GetType(State), "State String", bool ignoreCase);

The C++/CLI equivalent is

status->State = (States)Enum::Parse(States::typeid, gcnew String(cacheItem->state));

As you can see, cacheItem->state is a CString, not a System::String, so we need to quickly create a System::String for it.


No comments: