By now I should know better, but more often than care to admit, Enumerations in Xojo come back to bite me in the behind. Let me elaborate…
Even though they appear (and even claim) to be, as they are currently implemented, Enumerations in Xojo are do not consist of values of a fundamental type (i.e. Integer, or String). This often leads to the programmer casting enums into one of the standard variable types, making for much less readable code, exactly opposite of what they are designed to accomplish.
For example, I am writing a class to perform Histogram adjustments on an image. In general these consist of white point, midpoint and black point level modifications, using either the Red, Green, Blue or Intensity (averaged) channels of a picture object (if you’re confused, take a look at the Histogram tool in the image editor of iPhoto). I need to keep track of these three values for each of the four channels, so building enumerations like so:
Public Enum HistogramValues As Integer MinPoint = 0 MidPoint = 1 MaxPoint = 2 End Enum Public Enum HistogramChannels As Integer Intensity = 0 Red = 1 Green = 2 Blue = 3 End Enum
seems reasonable. This would allow keeping the actual level values in a 2 dimensional array called “Level” (actually since Xojo can’t handle Properties with parameters, we need to code this as separate Methods with the same name, one using the “Assigns” keyword, keeping the actual level values in a shadow array).
Dim mLevel(2, 3) As Integer Function Level(Value As HistogramValues, Channel As HistogramChannels) As Integer Return mLevel(Value, Channel) End Function Private Sub Level(Value As HistogramValues, Channel As HistogramChannels, Assigns Level As Integer) mLevel(Value, Channel) = Level End Sub
Alas, this doesn’t actually work, because Enumerated values can’t be used as indices in the array lookup (even though they are defined as “integers”, the compiler doesn’t think of them as such).
Sure we can cast them as integers (and that’s what most people do):
Function Level(Value As HistogramValues, Channel As HistogramChannels) As Integer Return mLevel(Integer(Value), Integer(Channel)) End Function Private Sub Level(Value As HistogramValues, Channel As HistogramChannels, Assigns Level As Integer) mLevel(Integer(Value), Integer(Channel)) = Level End Sub
but in my case, this is real time video, so this unnecessary “casting” is happening 30 times per second.
Answer is simple: get rid of all the value checking that occurs on enumerated variables at run time. Treat them as standard variable types (Integers or Strings) and allow us to use them the way they were intended (as a means of helping the programmer write more readable code), not as some weird user defined “pseudo-variable-type”.
The sad thing is that I’ve had this conversation before, and each time lots of really good Xojo programmers (who I highly admire) are quick to second my exasperation, but year after year nothing changes. I don’t think that changing this would break anybody’s code, but I absolutely know that it would make using Enumerations much more useful.
At least think about it.