Extend Enumerations

I have been making heavy use of Enumerations as of late and continue to be frustrated by their limitation to the integer datatype and had an epiphany to make converting enumerations easier using extension methods. I have yet to see this documented so it may be wholly unsupported and prone to breaking in future releases.

By using an extension, you can merely call SomeEnum.toString to convert an enumeration to a string (or whatever you wish). The enumeration and extension method both need to be able to see each other so you should put them in a module together or on the same class, but it seems to work for me in several cases with differing datatypes.

I hope this helps someone out there who has a mess of enumerations like I do!

Example:

[code]Public Enum SomeEnum
Foo
Bar
Baz
End Enum

Public Function toString(extends s as SomeEnum) as String
Select Case s
Case SomeEnum.Foo
Return “Foo”
Case SomeEnum.Bar
Return “Bar”
Case SomeEnum.Baz
Return “Baz”
End Select
End Function

Public Function toSomeEnum(extends s as String) as SomeEnum
Select Case s
Case “Foo”
Return SomeEnum.Foo
Case “Bar”
Return SomeEnum.Bar
Case “Baz”
Return SomeEnum.Baz
End Select
End Function
[/code]

Which allows for you to use code like this TheEnumeration.toString

This is also possible (and the way I’ve been doing it for a while on the recommendations by others on the forum) by writing a method like this:

Public Function SomeEnumToString(s as SomeEnum) as String Select Case s Case SomeEnum.Foo Return "Foo" Case SomeEnum.Bar Return "Bar" Case SomeEnum.Baz Return "Baz" End Select End Function
Which you would call using this code Dim s As String = SomeEnumToString( TheEnumeration )

While both work, I find the extension method to lead to more readable code.

Also, forgive me if this technique is widely used, I have never seen it mentioned.

Never apologize for posting tips. Aside from newbies, even experienced developers could benefit no matter how “widely used” something is.

Thanks for posting.

[quote=423203:@Kem Tekinay]Never apologize for posting tips. Aside from newbies, even experienced developers could benefit no matter how “widely used” something is.

Thanks for posting.[/quote]
Thank you Kem!

This really blew my mind so I uploaded my test app where I first tried this in case my initial post is confusing.

GitHub

This is a great tip. I liked it so much I’ve added a section about it to the Enumeration page. Thanks, Jonathan.

https://documentation.xojo.com/api/data_types/enumeration.html#Use_with_Extends

[quote=423211:@Paul Lefebvre]This is a great tip. I liked it so much I’ve added a section about it to the Enumeration page. Thanks, Jonathan.

https://documentation.xojo.com/api/data_types/enumeration.html#Use_with_Extends[/quote]

Thanks, Paul! It’s kind of cool to see that end up in the documentation, and I’m glad you added default values, I didn’t quite get to that point with my proof of concept.

I do have one question for you. What is the difference or advantage of using your version of converting a string to enumeration as opposed to using toSomeEnum(extends s as String) as SomeEnum? Is it just that it keeps the string datatype from having dozens of extension methods or is it better? I was not familiar with assigns or ByRef before looking at that code.

Hmm, I just discovered I made a mistake above. Extends cannot be used in class methods.

From the LR:
You can use Extends only for methods in a module.

@Paul Lefebvre You may want to update the new documentation since it includes my incorrect information from above.

XOJO Enums have frustrated me for a long time. I also have to create and maintain a ToString method for every enum I create. The fact that converting to and from the string value isn’t built-in like many other languages is pretty frustrating. It especially makes writing a generic serialization library very difficult, as you need to provide delegate methods if you want the enum to be able to serialize/deserialize from string values.

I wish XOJO would support an attribute you could add to the enum definition that would auto-generate and maintain the ToString method as well as include it with the Introspection TypeInfo for the object. Or maybe right click on an enum and click “Add string conversion”

Yes, fixed.

I was going for a Getter/Setter method set to keep things a little neater. And since this is for an Enumeration, it made sense to extend that rather than extend a String. It just fit better in my mental model.

[quote=423224:@Brock Nash]XOJO Enums have frustrated me for a long time. I also have to create and maintain a ToString method for every enum I create. The fact that converting to and from the string value isn’t built-in like many other languages is pretty frustrating. It especially makes writing a generic serialization library very difficult, as you need to provide delegate methods if you want the enum to be able to serialize/deserialize from string values.

I wish XOJO would support an attribute you could add to the enum definition that would auto-generate and maintain the ToString method as well as include it with the Introspection TypeInfo for the object. Or maybe right click on an enum and click “Add string conversion”[/quote]

Brock, I agree it would be nice for the enumeration support to be more robust. In fact, it should be. It would make enumerations more flexible and make the use of them more accessible to those who are new to programming and/or Xojo, but I hope this helps bridge that gap in the meantime.

I figured as much and already changed my implementation in my app and in my example app to mirror your design. Keeping extensions off of the more common datatypes seems like a good policy to maintain.

Hi there Jonathan,
Great input. How would you go about if you had to make eNums language sensitive?

I have implemented a Method ‘eNumInit’ that creates an Array for each eNum that I can then Array.IndexOf - but your method seems a bit ‘smarter’. Having languages to support I thought of a Memory SQLite DB that gets initiated at startup, where ‘SomeeNum.ToString’ could read from?

Would it be possible to make 1 (one) .toString Method that catches all eNums using ex Variant as parameter and the test for the eNum type? (I’m not that experienced in Xojo, so I don’t know the advanced stuff yet).

localized strings
http://documentation.xojo.com/topics/localizing_your_apps/introduction_to_app_localization.html

and no you dont want to try and extend EVERY enum using variant as that will extend EVERY type
enums define their own TYPE and to do much with them you have to extend that specific type

You might want to review this conversation. The refactoring tool is available free to all. Saves a heap of time doing the drudge work :slight_smile: