Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:08:14+00:00 2026-05-29T05:08:14+00:00

I have already set a variable in my document class Main.as. I am now

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:08:15+00:00Added an answer on May 29, 2026 at 5:08 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have my models set up already. class books(models.Model): title = models.CharField... ISBN
I have a problem with the global environmental variable CMAKE_PREFIX_PATH . I already set
Ok! I have a flashVar variable that is coming into Flash, its URL encoded
Is it possible to set environment variable with maven (OS: Linux)? I already have
I have already created a webpart to show the data from list, but I
I have a class holding complex scientific computations. It is set up to only
I have a fairly straightforward set of php forms that take users through a
I thought I already know when a variable has a value. But I was
I already have my pom.xml set up to do some basic replacing on a
Here is an example jade input: The class variable is undefined input.class(name=class, type=textfield, value=#{locals.class})

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.