-
Ronze
Very simple: Make property in function?
Hey everyone
A very simple question:
When you make a property inside a function, it's only inside that function that the property can read.
How do I make a property inside a function, that you can target from anywhere in the class and from other classes?
Like when I make a property just inside the class definition....
-
half as fun, double the price
If you want to make it accessible everywhere, you would have to define it in an object that is accessible everywhere. For that you can use a separate class and make the property a static property of that class. Then to access that property anywhere, you would use
MyClass.myProperty
-
OOP is one letter from OOPS
Is there some reason you are not declaring an instance variable in the class for this value?
-
At the cost of invoking the dreaded dynamic penalty, you could make your class dynamic, then instead of saying
Code:
function someFunction():void{
var someprop:String = "someValue";
}
you could do:
Code:
function someFunction():void{
this.someprop = "someValue";
}
Or, if you don't want your entire class to be dynamic (good idea if performance is an issue, or you care about clean design), you could store these dynamic properties in an Object especially for that purpose.
Code:
public class MyClass{
private var dynProps:Object;
public function MyClass(){
dynProps = new Object();
}
public function getProperty(propName:String):Object{
return dynProps[propname];
}
public function setProperty(propName:String, propVal:Object):void{
dynProps[propName] = propVal;
//may want to auto delete nulls here, depending on your logic
}
public function someFunction():void{
setProperty('someprop', 'someValue');
}
}
-
Ronze
Hey, and thanks for your answers....
But I'm not sure you understand. I have a class called Fruit and a MainClass. In the MainClass I want to make a new instance of fruit, and call a Fruit.makeFruit() method. When this method is called I want to make 1 single property in the Fruit object that I can reference from my MainClass like this:
Fruit.someProperty.
But my problem is, that If I just declare the property normally inside the function, I cannot read the property in my MainClass, because of the function scope. But the property HAS to be made when I call the function, and there must not be any code outside the function.
In AS I just did it like this:
_root.myProperty = "hello";
Because then it would make the var accesible from anywhere in my movie.
-
Why are you creating a new property inside a function? Whats wrong with:
Code:
public class Fruit {
public var someProperty:String;
function setProperty (s:String):void {
someProperty = s;
}
}
Since its public you don't even need the setter function.
There is no valid reason to create a new property inside a function. Dynamic classes are terrible programming style and are unnecessary.
The greatest pleasure in life is doing what people say you cannot do.
- Walter Bagehot
The height of cleverness is to be able to conceal it.
- Francois de La Rochefoucauld
-
Ronze
I think you are wright... I think i figured i out.... thanks alot!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|