views:

525

answers:

3

Hi I'm wondering if I can have a packageless () AS3 class call a private method on the main class in the file. For example:

package demo
{
    public class MyDemoClass
    {
        var helper:FriendlyHelperClass = new FriendlyHelperClass(this)
    }

    private function methodToCall():void
    {
        ...
    }
}

public class FriendlyHelperClass
{
    public function FriendlyHelperClass(demo:MyDemoClass)
    {
        demo.methodToCall()
    }
}

The call to methodToCall() from FriendlyHelperClass will fail as it is a private member of the MyDemoClass. Is there any way to call the methodToCall() method from the FriendlyHelperClass without extending MyDemoClass.

Basically I'm looking for inner class functionality that Java has or some sort of C++ style friend class.

A: 

I'm afraid that is not possible, but if you make the class dynamic, then you can edit it while the program is running, and possibly add some useful functions to it, to access the private functions. I haven't tried it though.

Dynamic classes

Marius
+2  A: 

Short answer : no.

You can never access a private member from outside a class in ActionScript. What you could do is use a namespace instead of a private scope. This would allow to give access to some members to selected classes. This is the closest of a friend class that you will get in AS3.

Mexican Seafood
Sounds interesting, do you have a small example using namespaces maybe?
Brian Heylin
Hi, sorry for the lag of my response.One way to do this would be to create an internal namespace like this :package{ internal namespace ns;}And instead of doing private function ...(), you do ns function ...().To access these, you do myFriendClass.ns::funcName().Of course this only works inside of a single package. You could make the namespace public and have so-so encapsulation. Another way of doing it is declaring the namespace privately inside of a class and have this class create its friends, passing them the namespace. But that would mean a one way communication...
Mexican Seafood
A: 

Without testing the code, and knowing what your full problem. you can try passing the functions you need into the embedded class as a callback. e.g.,

helper.methodToCallCallback = this.methodToCall;

then inside FriendlyHelperClass:

this.methodToCallCallback();
Glenn