Can I return a resource from a string?

I need help converting a string into a resource, basically. For example:

active_icon = WatchUi.loadResource(Rez.Drawables.Red_Active);
I have various colors. So if I can somehow change the Red into Pink or other colors it would be great.
I know in other programming languages I can use brackets to process a string that I can then use in
a variable name. I can't remember what it is called to look it up.
Is there a way I can use: Rez.Drawables[:Red_Active] or something?
Thanks.
  • You can't convert a string directly to a symbol, because symbols are identifiers which are resolved at compile-time, not run-time.

    But you can use a string (or enum, integer, etc) to look up a symbol in a dictionary, array, switch statement, series of if statements, etc.

    e.g.

        enum {
            APP_COLOR_RED = 0,
            APP_COLOR_GREEN = 1,
            APP_COLOR_BLUE = 2
        }
    
        // color = APP_COLOR_*
        function getDrawable(color) {
            // an array lookup by index isn't the safest way to do things,
            // but it's one of the most memory-efficient.
            // of course if you need to look up symbols based on more than
            // one criteria (like color and state), you will need
            // something more complicated than this
            var colorSymbolTable = [
                :Red,
                :Green,
                :Blue
            ];
            var colorSymbol = colorSymbolTable[color];
            return WatchUi.loadResource(Rez.Drawables[colorSymbol]);
        }