• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Noob question - help with understanding what is public and private variables?

Valorous Hero ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

As far as I understand public variables can be used from other classes. But how? I'm trying this without a success:

create a class:

package {
    public class MyClass {
        public var myVariable:Number = 5;
        public function MyClass() {
            trace(myVariable);
        }
    }
}

then create another class:

package {
    public class TryThis {
        public var myResult:Number = myVariable;
        public function TryThis() {           
            trace(myResult);
        }
    }
}

and I got an error "undefined property myVariable" when using this in the Flash file:

var niceTry:TryThis = new TryThis();

Will greatly appreciate your help.

TOPICS
ActionScript

Views

669

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 12, 2010 Feb 12, 2010

use:


package {
    public class myClass {
        public var myVariable:Number = 5;
        public function myClass() {
            trace(myVariable);
        }
    }
}

then create another class:

package {

import myClass;
    public class TryThis {
        public var myResult:Number;
        public function TryThis() {

var mc:myClass=new myClass();

myResult = mc.myVariable;

trace(myResult);
        }
    }
}


Votes

Translate

Translate
Community Expert ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

use:


package {
    public class myClass {
        public var myVariable:Number = 5;
        public function myClass() {
            trace(myVariable);
        }
    }
}

then create another class:

package {

import myClass;
    public class TryThis {
        public var myResult:Number;
        public function TryThis() {

var mc:myClass=new myClass();

myResult = mc.myVariable;

trace(myResult);
        }
    }
}


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

Thank you kglad,

this works but my understanding about the purpose of using public vs private is still very cloudy. For example if I change public variable to private in the first class it still works and doesn't make a difference with this example.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

no it won't.  you'll only see 1 trace() output after making that variable private, not 2.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

Oops, sorry. I've made the test with a wrong class. Now I see what you mean.

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

Sorry for bringing this again - I just want to confirm my understanding of the public variables. So, am I right to think that it is not possible to get a variable from another class without also getting the result from the main function of that class?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

LATEST

for non-static variables yes, any variable defined in the scope of a class instance is only available by referencing that class instance.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines