Skip to main content
Inspiring
June 10, 2011
Answered

Using "this" - or not? Accessing properties of a member of a class from within the class.

  • June 10, 2011
  • 1 reply
  • 773 views

I have a class, call it MyClass, and there are several variables that I need to be able to access each instance of from within the class.

So for instance, I need the object myObject1, a member of MyClass, to store the variable myNumber.

From the main timeline, if I trace myObject1.myNumber, I get whatever number I've stored.

However, I need to check the numbers against another parameter, so I need to be able to test if myObject.myNumber == myOtherNumber. But from within the class, if I trace this.myNumber or myNumber I get null.

What is the proper syntax for accessing a variable within a class?

This topic has been closed for replies.
Correct answer kglad

look for a timing issue, where you're trying to access the variable in your class before it's defined (on the main timeline).  the following would be a typical error:

var yc:YourClass=new YourClass();

yc.var1="HI";

///////////


package{

import...

public class YourClass...

.

public function YourClass(){

//any attempt to access this.var1 here (the constructor) will fail

}

}

}

1 reply

AmbariAuthor
Inspiring
June 10, 2011

After some more testing it doesn't look like it's the accessing of the variables but rather when I set them in the first place.

In the main timeline, if I do a trace (myObject.myNumber) it will  return myNumber. Within the class, if I do a trace of (this.myNumber) it  returns null.

However, if I add a line of code in my class and add this.myNumber = 3, then trace (this.myNumber) traces 3.

So it's obviously not the "this." that's the problem. Somehow my declaration in the maintime of myObject.myNumber = 3 is not actually initializing it properly.


What am I doing wrong? Why can't I get myObject to correctly change its own parameters?

Lee_Burrows
Participating Frequently
June 10, 2011

hi

post the code for your class