Search code examples
actionscript-3flash-cs5

Read a variable from a different Class in Actionscript 3


I have already set a variable in my document class "Main.as". I am now trying to access that variable and read its value from a different Class and Function, take that value and email it.

For example in my "Main.as" file I have this function:

public var _myVar:String;

function create() {
    _myVar = "hello";
}

Now from my other class "EmailtoFriend.as" I have a new function to try and get the value of that pre set variable:

function getVar() {
    trace(_myVar);
}

Why will it not output "hello"? Instead I get an error saying: Access of undefined property _myVar. If I could just get this simple example working, I think it will help me understand a lot of things. Thanks!


Solution

  • The error your getting really says it all. Although _myVar is defined in your Main class public var _myVar:String;, it isn't defined in your Emailtofriend class. If you want access to _myVar you need to do one of the following:

    Parse a reference of your Main object(using this) to your EmailToFriend class:

    Main.as(document class):

    package 
    {
        import flash.display.Sprite;
        import flash.events.Event;
    
        public class Main extends Sprite 
        {
            public var _myVar:String;
    
            public function Main():void 
            {
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
    
            }// end function
    
            public function create():void
            {
                _myVar = "hello";
    
            }// end function
    
            private function init(e:Event = null):void 
            {
                removeEventListener(Event.ADDED_TO_STAGE, init);
    
                create();
    
                var emailToFriend:EmailToFriend = new EmailToFriend(this);
                emailToFriend.getVar();
    
            }// end function
    
        }// end class
    
    }// end package
    
    internal class EmailToFriend
    {
        private var _main:Main;
    
        public function EmailToFriend(main:Main)
        {
            _main = main;
    
        }// end function
    
        public function getVar():void
        {
            trace(_main._myVar);
    
        }// end function
    
    }// end class
    

    Or to make _myVar a public static property of Main and access it via Main._myVar:

    Main.as(document class):

    package 
    {
        import flash.display.Sprite;
        import flash.events.Event;
    
        public class Main extends Sprite 
        {
            public static var _myVar:String;
    
            public function Main():void 
            {
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
    
            }// end function
    
            public function create():void
            {
                _myVar = "hello";
    
            }// end function
    
            private function init(e:Event = null):void 
            {
                removeEventListener(Event.ADDED_TO_STAGE, init);
    
                create();
    
                var emailToFriend:EmailToFriend = new EmailToFriend();
                emailToFriend.getVar();
    
            }// end function
    
        }// end class
    
    }// end package
    
    internal class EmailToFriend
    {
        public function EmailToFriend() {}
    
        public function getVar():void
        {
            trace(Main._myVar);
    
        }// end function
    
    }// end class
    

    Also one small thing, when using underscores for class properties, you should only use them for private properties, not public. Well I say only but I really mean it's more common.

    [UPDATE]

    This is in response to your comment:

    Main.as:

    package 
    {
        import EmailToFriend;
        import flash.display.Sprite;
        import flash.events.Event;
    
    
        public class Main extends Sprite 
        {
            public static var _myVar:String;
    
            public function Main():void 
            {
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
    
            }// end function
    
            public function create():void
            {
                _myVar = "hello";
    
            }// end function
    
            private function init(e:Event = null):void 
            {
                removeEventListener(Event.ADDED_TO_STAGE, init);
    
                create();
    
                var emailToFriend:EmailToFriend = new EmailToFriend();
                emailToFriend.getVar();
    
            }// end function
    
        }// end class
    
    }// end package
    

    EmailToFriend.as:

    package  
    {
        import Main;
    
        public class EmailToFriend 
        {
            public function EmailToFriend() {}
    
            public function getVar():void
            {
                trace(Main._myVar);
    
            }// end function
    
        }// end class
    
    }// end package