Enum Item Names Via introspection?

I have not done much with introspection so I may be missing something … I would like to do something that, looking at the docs i don’t see a way to do, but maybe there is one…

i would like to be able to get the names as strings (and corresponding values) for items defined in an Enum. That way I can only define the list once but use the Enum in code and be able to create popup menus for the enum without having to essentially create and maintain 2 lists of the options.

Is this doable? If so can someone point me in the right direction?

Thanks,

  • karen

No (unfortunately).

You could create a dictionary with the enums as keys and the names as values to be able to get the string representation.

Or you could use a workaround by casting the enumeration to the integer value and by taking the integer value of the enumeration on the other side.

Actually as the Enum item values in order I will just use and array. I was just hoping not to have to maintain 2 lists, as I am 90+ percent sure that I will need to add more items to the Enum over time… And in general being able to do that would be very convenient

Thinking about it, If there was a way to do that I’m sure they would have used it for setting up enum values in the property inspector instead of the way they did it.

Thanks,

  • karen

That does not get me the names of the enum items as a string, which is the core of what I want.

  • karen

Enum names dont exist at runtime
Much like constants they are basically folded to literal values
They are type checked at compile time

Given this declaration

Enum Foo
   bar
   baz
end Enum

And you initially write

dim something as Foo = Foo.baz

It’s as if at runtime your code was actually

dim something as integer = 1 

I know. That’s why its only a kind of workaround where you don’t want translation methods or a dictionary, and which works only well if your enum has no gaps.

[quote=296993:@Norman Palardy]Enum names dont exist at runtime
[/quote]

Have you (Xojo Inc) considered doing something under the hood (not necessarily with introspection system) so we could get the item Names as strings (and values for each) and maybe also allowing us to define a string for each item (with String constants allowed for localization) and when one is not provided return the Enum name?

That way enums could be used the IDE inspector pane as well as in our code (and our UI’s) directly without a lot of fuss/duplication and additional coding on our end.

That would make Enums a special animal, but it would be a helpful…

  • Karen

What the IDE can do (since it has source code) and what the compiler do are not connected
The IDE has the entire source code and can already se enums in the inspector

I know there is a request to add introspection data for enums but thats as much as I can/will say

[quote=297001:@Norman Palardy]What the IDE can do (since it has source code) and what the compiler do are not connected
[/quote]

Of course … but you CAN auto generate code based on the ENUM definition in the IDE that is then fed to the compiler at compile time (which is what I meant by “Special Animal”… kind of like Windows and some other things are).

I can think of several ways that could be done.

[quote]
I know there is a request to add introspection data for enums but thats as much as I can/will say[/quote]

That would require a major change to the design of introspection as it currently works only on objects and not classes or modules. That of course would be useful too…

  • Karen

[quote=297002:@Karen Atkocius]Of course … but you CAN auto generate code based on the ENUM definition in the IDE that is then fed to the compiler at compile time (which is what I meant by “Special Animal”… kind of like Windows and some other things are).
[/quote]
This just introduces other problems

Also note that Enum names are much more restricted than a simple text name would be… eg no spaces. So this would be a lot of work, runtime overhead for relatively little value, especially since we can already create our class that does most of this ourselves.

Create a new class, call it EnumList that will hold a list data records (array or dictionary depending on your lookup preference)
Create a data record class, call it EnumRec, with only a few properties (EnumValue, EnumName, AnotherEnumProperty, etc)
In the Constructor for EnumList add as many EnumRec items as you need in your enum. Note that you’re defining the whole list in just one place, which was your original request.
Add a few simple lookup methods to EnumList to suit your preferred access methods (ByName, ByID, etc)
You could even use an actual ENUM as the ID if you want to have a bit more type checking at compile time vs just the runtime lookups

Anyway this isn’t hard to add to our own projects, and allows us the flexibility to extend it to include a few more properties than just an integer and a very restricted name as an enum does.

ENUM ITEM NAMES VIA INTROSPECTION
There is more Latin than English in this sentence :wink:

Actually what I would like would not actually be enums per say but “constant Groups” that can be any data type that constants now support, that could be defined at the the class, module or even the interface level (ALL WITH SCOPES) as a way to organize options. That would help with AutoComplete making the details more discoverable, and the navigator more manageable. I see these most often used as options for method parameters, but I can envision other uses as well.

[code]ConstantGroup DoOptions as Integer
Do1 = 1
Do2 = 2
Do3 = 3
Do4 = 4
End Constant Group

ConstantGroup Spacing as Double
Quarter = 0.25
Half = 0.5
ThreeForths = 0.75
Full = 1.0
Double = 2.0
End Constant Group
[/code]

Well you get the idea.

Unlike Enums the values would not be a there own type so they could be assigned directly to variables of that type… Something like Constant Structs. But I could live with them being their own type…

And yes I think we should be able to define constants and Structs on interfaces… IMO they should be able to used as namespaces.

BTW speaking of organization in teh Navigator or even in the old RB IDE, I would love to be able to organize constants, methods, properties etc within their sections into sub folders

Anyway my 2 cents.

  • Karen

hi Karen

Yes, those are all very handy ideas.

I agree an important issue is to make organizing groups of Constants easier, rather than just listing them alphabetically.

Cheers,
Joe

BTW I forgot I was the person who started this thread… My ideas don’t solve my initial need… but since it was not doable I moved on.

My last post was about ideas I’ve had for years and mentioned before.

  • Karen