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.
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);
}
}
}
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);
}
}
}
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.
Copy link to clipboard
Copied
no it won't. you'll only see 1 trace() output after making that variable private, not 2.
Copy link to clipboard
Copied
Oops, sorry. I've made the test with a wrong class. Now I see what you mean.
Thanks
Copy link to clipboard
Copied
you're welcome.
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?
Copy link to clipboard
Copied
for non-static variables yes, any variable defined in the scope of a class instance is only available by referencing that class instance.