Brandon
Brandon

Reputation: 81

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!

Upvotes: 0

Views: 4225

Answers (5)

Jakir Hossen
Jakir Hossen

Reputation: 461

What you should do is have a OOP approach, meaning use encapsulation in your classes. If you don;t know what that means, its ok. For instance, if you have a variable, that you want to be accessible, then you should really make it private, and the set up its own public function that returns the variable. Like this:

package {

public class SomeClass {
    private var someVar:Number = 12; // A private variable, which means only this class can
// use the reference someVar, and only other outiside classes can use the function getSomeVar.
    ... // skip some regular class stuff

    public function getSomeVar():Number {
        return this.someVar; //returns the variable someVar from this class to whoever is accessing it. 
//This function is public which means that anyone can call it and get the variable someVar.
    }
}
}

To access that variable, you just reference a class instance:

var someClass:SomeClass = new SomeClass(); // create the instance using the variable someClass
var myVar:Number = someClass.getSomeVar(); // ACCESSES the variable that you want from the class, 
//by first using the class instance reference, and then calling its public function that returns the value you want.

Upvotes: 0

Taurayi
Taurayi

Reputation: 3207

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

Upvotes: 1

Goran Mottram
Goran Mottram

Reputation: 6304

All variables implicitly have a target, unless explicitly specified. Variables without an explicit target will commonly look in the local scope of the function (in this case, getVar()) and the global scope of the class (in this case, EmailToFriend).

I assume that these don't exist in your code, judging by the error. You will need something like the following to access the var:

function getVar() {
    var main:Main = new Main();
    main.create();
    trace(main._myVar);
}

Upvotes: 1

mgraph
mgraph

Reputation: 15338

  package {
    import flash.display.MovieClip;  
    public class Main extends MovieClip  {
       public var _myVar:String;
       public function Main(){
         create();
       }
       private function create() {
          _myVar = "hello";
         }
      }
      }
    }

in EmailtoFriend.a

import Main.as
var myMain = new Main();
trace(myMain._myVar);

Upvotes: 0

crooksy88
crooksy88

Reputation: 3851

Assuming Main.as is your document class:

public var _myVar:String;

public function create():String {
//we specify that this function will return a String using the :String in the above line

    //now give the variable a value
    _myVar = "hello";

    //and send it back to the caller of the function
    return _myVar;
}

Within your other class

function getVar():void {

    //MovieClip(root) is another way of referencing your document class.
    trace(MovieClip(root).create());
}

OR...

public var _myVar:String;

public function create():void {

    //now give the variable a value
    _myVar = "hello";

}

Within your other class

function getVar():void {

    //call the function that actually gives myVar a value
    MovieClip(root).create();

    //now you can trace the value
    trace(MovieClip(root)._myVar);
}

Upvotes: 0

Related Questions