Enum of type String

Should I be able to add a new Enumeration and set its type to String in the Inspector? Every time I do this it flicks back to Integer…

The underlying type of an enumeration must be one of the integer types. If the IDE displays string as an option, it should be filed as a bug report.

Thanks Joe, I don’t suppose there is any way to get the string version of an enum name?

No.

You can always use and store the enum value (usually better than static strings) and have a shared string array property on your class. Then create a XyzName type computed property. You can easily create the Get and Set based on the array and enum value. For example, in use:

p.Role = Person.Roles.Dad
MsgBox p.RoleName  ' "Dad" from the RoleName array

If you need help with the internals, let me know. A hint is you should make the mRoleName() array a shared property and in the constructor, check to see if it has been initialized or not. If not, of course populate it. This will cause it to be populated only once and stored only once in memory no matter the number of instances of your class you may have.

Jerymy how did you do that ?

@Kato Gangstad it’s not as fancy as you may think. Say you have an enum Roles that is defined in a module named “Models”, meaning database models. So your Roles enum contains:

Dad, Mom, Brother, Sister, Uncle, Aunt, Grandma, Grandpa, ...

Then I just created a method in that Models module:

function RoleToString(role as Roles) as String
    static roleNames() as String = Array("Dad", "Mom", "Brother", ...)
    return roleNames(Int32(role))
end function

So, now I can use in code: Models.Roles as the enum, and when I need to know what an enum represents in human terms, Models.RoleToString(Models.Roles.Dad).

You have to maintain two lists, it’s not ideal. All it does is centralize the RoleToString handling with a static array so you do not have to do it in multiple places in your code.