[SOLVED] Publicly available enum?

Hi all,

I’ve never used enumerators before, so I’m very unfamiliar with how to use it. I see that in a .hx file I have to declare it before the class, but then that seems to mean it’s only available for that class. Adding an access modifier didn’t do anything to it.

Is there a way to make the enum constructors available from other classes than that in the same .hx file?

Make a file with the same name as the enum. For instance, if this is your enum…

package com.example.mypackage;

enum Color {
    RED;
    GREEN;
    BLUE;
    CYAN;
    MAGENTA;
    YELLOW;
}

…you’d put it in a file named “Color.hx,” in the “com/example/mypackage” folder.

I see! I can get it to work, but it feels a little hacky. If I remember correctly, in C# you have to declare an enum at the same level as class, within a file, but you can also expose it by making it public.

Thanks for the info player_03!

Why does it feel hacky? It’s the same as declaring a class or interface: just put it in its own file.

You can bundle it into another file if you like, but as you discovered, it’s harder to refer to it. You’d need to import com.example.mypackage.MyClass.MyEnum or just com.example.mypackage.MyClass. (Warning: the latter import statement may get removed by FlashDevelop if you don’t have any other references to MyClass.)

Aaah OK, I see! It felt hacky because of having to create a file just for the enum, while I have previously only encountered enums as properties of classes. But in Haxe they’re on the same “level” as classes and interfaces. Thanks for clearing it up for me :slight_smile:

I’ve switched over to using the enum now, instead of a class consisting of constants. Thanks!