A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Very simple: Make property in function?

  1. #1
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185

    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....
    --- Ronze ---

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    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

  3. #3
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Is there some reason you are not declaring an instance variable in the class for this value?
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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');
      }
    }

  5. #5
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185
    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.
    --- Ronze ---

  6. #6
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    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

  7. #7
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185
    I think you are wright... I think i figured i out.... thanks alot!
    --- Ronze ---

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center