I’ve been complaining about this for years. It makes it difficult to write serialization libraries without it.
My first suggestion is to make a helper extension method that knows how to convert from the type to its string.
Protected Function ToString(extends ChartType as ChartTypes) As string
Select Case ChartType
//MOST COMMON FIRST FOR IMPROVED PERFORMANCE
Case ChartTypes.PieChart
return "PieChart"
Case ChartTypes.BarChart
return "BarChart"
Case ChartTypes.AreaChart
return "AreaChart"
Case ChartTypes.LineChart
return "LineChart"
...
End
End Function
Here’s another way I use when I don’t want to write the select case.
Every time I create an enumeration I actually create two methods now (This is an example for an Enum called AccountType)
One to return IN ORDER the list of all the string names (you do need to keep this list up to date)
Function AccountTypes() As string()
return Array("AccountsPayable", "AccountsReceivable", "Bank", _
"CostOfGoodsSold", "CreditCard", "Equity", "Expense", "FixedAsset", "Income", "LongTermLiability", _
"NonPosting", "OtherAsset", "OtherCurrentAsset", "OtherCurrentLiability", "OtherExpense", "OtherIncome")
End Function
And one to convert the enum to its string
Function toString(extends a as QBD.AccountType) As String
dim v as Variant = a
try
dim options() as string = AccountTypes
return options(v.IntegerValue)
catch
return ""
end
End Function
Sometimes I include a “NilOption” in my list. If so add this to the top and say “NilType” = -1,
That way they indexing of the other options still starts at 0 so they array matches up for their names.
This is also nice because you can fill a listbox with all the choices of an enum as well.
For i as integer = 0 to AccountTypes.ubound
listbox1.addRow(AccountTypes(i))
next
And if you want to set the rowTags to be the actual enum values, you just cast “i” into your enum type.
At the very least Xojo should let us generate these two “special methods” and have them just automatically stay up to date. We could then manually pass all these Methods into a serialization class so it could resolve these types by name instead of int value.