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!
The error your getting really says it all. Although
_myVaris defined in yourMainclasspublic var _myVar:String;, it isn’t defined in yourEmailtofriendclass. If you want access to_myVaryou need to do one of the following:Parse a reference of your
Mainobject(usingthis) to yourEmailToFriendclass:Main.as(document class):
Or to make
_myVara public static property ofMainand access it viaMain._myVar:Main.as(document 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:
EmailToFriend.as: