using enum in a different class

hi,

i am trying to use an enum in a different class (not the class which defined it).
my code is similar to this:

class classA
{
enum
{
SCREEN_1,
SCREEN_2
}

function fA()
{
var screen = SCREEN_1;
Sys.println("classA:"+screen);
}
}

class classB
{

function fB()
{
var screen = classA.SCREEN_1;
Sys.println("classB:"+screen);
}
}


the output ot this is:

classA:0
classB:class


i have tried to use toNumber(), but i get an exception.
i have also defined the enum as static, but this did not help either.

any idea?
thank you in advance
  • Looks like a bug. The Programmer's Guide says that you must declare an enum at class or module scope, so you are following the language requirements.

    Here is a complete test case based on the code you've provided above.

    using Toybox.System as Sys;
    using Toybox.Application as App;

    class A
    {
    enum {
    SCREEN_1 = 1,
    SCREEN_2 = 2
    }

    function f()
    {
    var val = SCREEN_1;
    Sys.println(val);
    Sys.println(1.equals(val));
    }
    }

    class B
    {
    function f()
    {
    var val = A.SCREEN_1;
    Sys.println(val);
    Sys.println(1.equals(val));
    }
    }

    class MyApp extends App.AppBase
    {
    function initialize() {
    AppBase.initialize();
    }

    function getInitialView() {

    var a = new A();
    a.f();

    var b = new B();
    b.f();

    return null;
    }
    }


    Here is the output...

    Device Version 0.1.0
    Device id 1 name "A garmin device"
    Shell Version 0.1.0
    1
    true
    class
    false
    Complete
    Connection Finished
    Closing shell and port


    I did notice that if I get the enumerator value via an object reference, everything works as expected...

    // in B.f()
    var val = new A().SCREEN_1;


    You really have two options to work around the problem. You can either use const (static const) members in class A...

    class A
    {
    static const SCREEN_1 = 1;
    static const SCREEN_2 = 2;

    // ...


    or you can move the enumerator to a module or global scope.

    Travis
  • What I do, is make the enum global:
    enum
    {
    SCREEN_1,
    SCREEN_2
    }


    class classA
    {
    function fA()
    {
    var screen = SCREEN_1;
    Sys.println("classA:"+screen);
    }
    }

    class classB
    {

    function fB()
    {
    var screen = SCREEN_1;
    Sys.println("classB:"+screen);
    }
    }


    you could also assign in to a var:
    class classA
    {
    enum
    {
    SCREEN_1,
    SCREEN_2
    }
    var Screen_1=SCREEN_1;
    var Screen_2=SCREEN_2;

    function fA()
    {
    var screen = Screen_1;
    Sys.println("classA:"+screen);
    }
    }

    class classB
    {

    function fB()
    {
    var screen = classA.Screen_1;
    Sys.println("classB:"+screen);
    }
    }
  • What I do, is make the enum global:

    you could also assign in to a var


    Thanks Jim, the first approach (global) does not work if the classes are not in the same file, does it?
    Your second approach is approximately what I have. I Have defined consts, instead of an enum. The implementation with an enum was more elegant for me. But if I need an additional variable in the end, I may as well get rid of the enum.

    It is strange though.
  • I've used global enums in one source file from another source file many times.

    IIRC, there was a time when the file where they were defined had to be alphabetically before the 2nd file, but I just checked and that's not the case now. (if you had a.mc and b.mc, you had to define them in a.mc if you wanted to use them in a.mc and b.mc)

    In your code snip, you should be able to have the enums and classA in q.mc and classB in b.mc for example.
  • thank you guys,

    TRAVIS, somehow i missed your post this morning. i agree with you, it looks like a bug. i have struggled with this a couple of days.
    i dont like the idea of making the enum global. it would look like somewhat untidy for me. and i dont like to use the bling symbol, which is recommended for global variables. this is subjective, i know, but i dont like the bling.

    i will go on with the const.

    if some connectiq guy reads this, it might be advisable to open a ticket. travis code is all they need.

    thanks again.
  • thank you guys,

    TRAVIS, somehow i missed your post this morning. i agree with you, it looks like a bug. i have struggled with this a couple of days.
    i dont like the idea of making the enum global. it would look like somewhat untidy for me. and i dont like to use the bling symbol, which is recommended for global variables. this is subjective, i know, but i dont like the bling.

    i will go on with the const.

    if some connectiq guy reads this, it might be advisable to open a ticket. travis code is all they need.

    thanks again.


    Yep! Just opened one. I was able to confirm this with the code provided and we will take a look at it this week.

    Thanks,
    -Coleman