PDA

View Full Version : properties in a user created class



skjc
September 16th, 2005, 11:53 AM
If I create a class and set up one property which is a variable initiated at the start of the class but not within a function or a constructor and I reference the property from the .fla in a for loop as:

Book is the class and myBook is the new object.

myBook:Book = new Book(); // (fla script).

for(var prop in myBook) {
trace(prop);
}

the .AS contains

Class Book
{

var myProp:Number = 0;

function Book() {
}

}

The trace does not return anything. However when I include myProp (the class variable) in a function (other than Book function) and call that function from the .fla then the trace works and I can see myProp.

I would be grateful if someone could explain: Do I need to include Class variables in called functions (methods of the Class) in order for them to be properties of the Class?

thanks in advance

jwopitz
October 25th, 2005, 08:32 PM
First off I recommend using things like:


private var internal_prop:Number = some#;

then you could make a method like:


public function get prop():Number{
return internal_prop;
}

this way you can then say:


trace(myClassInstance.prop)

This is also considered proper Object Oriented Programming. Hope that helps

senocular
October 26th, 2005, 06:45 AM
First off I recommend using things like:


private var internal_prop:Number = some#;

then you could make a method like:


public function get prop():Number{
return internal_prop;
}

this way you can then say:


trace(myClassInstance.prop)

This is also considered proper Object Oriented Programming. Hope that helps

But thats often pointless, more trouble than its worth, and does nothing but introduce more function overhead. I wouldn't do that unless other actions are required within the setting or getting operations. ;)

---

As for enumeration, you would need to define your property in your constructor. All definitions in the class body are assigned to the prototype which is hidden from enumeration.