views:

55

answers:

3
var image:Image = new Image();
image.property_1 = "abcdefg";

it can't compile as the Flash builder says:

Description  Resource     Path      Location       Type
1119: Access of possibly undefined property jdkfjds through a reference with static type mx.controls:Image.
             adm.mxml     /adm/src  Line 209       Flex Problem

How can I do that? Or I need to extends the Image class? It's quite boring.

+3  A: 

Image is not declared as dynamic class so you can`t dynamically add properties.

Derive from Image and extend the class with the dynamic keyword:

package mypackage
{
  import mx.controls.Image;

  public dynamic class DynamicImage extends Image {}
}

and now this will work:

import mypackage.DynamicImage;

var image:DynamicImage = new DynamicImage();
image.property_1 = "abcdefg";
splash
Is there a way I can do?
Bin Chen
A: 

But you can use container for your image and add extra properties there; or use dictionary with image as a key and property as a value.

alxx
can container receive the MouseEvent.CLICK event?
Bin Chen
Actually, subclassing is very easy as splash showed, I like this approach better.
alxx
+4  A: 

If you know all the attributes you need to add and they won't change (often), this will work.

package mypackage
{
    import mx.controls.Image;
    public class ExtendedImage extends Image
    {
        public function ExtendedImage()
        {
            super();
        }

        //option 1
        private var prop1:String = "";
        public function get property_1():String
        {
            return prop1;
        }
        public function set property_1(val:String):void
        {
            prop1 = val;
        }

        //option 2
        public var property_2:String = "";
    }
}
Jason W