I’ve got an object declared and instantiated in my Flex application’s singular MXML file:
public var CDN:CDNClass = new CDNClass;
I would like to access this same CDN object (and its public methods and properties) in another class declared in a separate .as file as such:
package my.vp
{
import my.media.CDNClass;
public class SyncConnectorManager
{
private function syncMessageReceived(p_evt:SyncSwfEvent):void
{
switch (p_evt.data.msgNm)
{
case "startStream" :
// Play a stream
CDN.parsePlayList(p_evt.data.msgVal);
break;
But when I try to access the public method parsePlayList in the CDN object in a method in the class defined in the .as file, I get the following error:
Access of undefined property CDN
The reason I want to do this is to break up the logic of my application into multiple AS files and have minimal MXML files, probably only one.
Thanks – any help is much appreciated. Perhaps my OOD/OOP thinking is not correct here?
Yes, your OOP thinking is not correct here. You should take in mind differences between classes and instances. This line declares a filed in a current class and initiates it with an instance:
So current instance of your MXML class (you can think about it as usual AS class with some other notation) has public field. To operate with
CDNinstance you need something from the following:CDN(as far as it is public) from the instance of your MXML class. You need some reference to it for that.SyncConnectorManagerandSyncConnectorManagershould have a way to inject the value ofCDNthere. Something like:Your class:
In your case
SyncConnectorManagerclass hasn’tCDNdeclared (the problem of the compiler error you mentioned) and instantiated (the problem of NPE even if you just declare field).As the bottom line I can suggest you to follow ActionScript naming and coding conventions to talk other people and team members about your code 🙂