views:

42

answers:

2

I have two MXML component files and try to work with them as classes. One of them has a simple function:

GUIFriend.mxml

<mx:Script>
    <![CDATA[           
        public function createName(f:Friend) {
            return 'friendProfile: ' + f.uid;
        }
    ]]>
</mx:Script>

And the other tries to use it:

GUIFriendContainer.mxml

<mx:Script>
    <![CDATA[

        import GUIFriend;

        public function getFriendProfile(f:Friend):GUIFriend {
            var result:DisplayObject = getChildByName(GUIFriend.createName(f));
            if (result is GUIFriend) {
                return result;
            } else {
                // TODO: throw error
                return null;
            }
        }           
    ]]>
</mx:Script>

But in the line that references 'createName' function I get two errors:

  1. Call to a possibly undefined method createName through a reference with static type Class. - (update) I forgot to make the method static.
  2. Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type GUIFriend.

But I see no rational reason for it. What's wrong with my code?

+1  A: 

You're trying to call an instance method directly through the class name.

You either need to make the method static or create an instance of the class and call the method on that.

DanK
Thanks! That was the issue with error 1.
+2  A: 

For second problem, try

var result:DisplayObject = getChildByName(GUIFriend.createName(f)) as DisplayObject;

if you still have problems, temporarily type result as Object and put in a debug breakpoint to check what is really coming back from the getChildByName call.

David Collie
That solved it, thanks!