One technique I often use is to create the array as a private member of
a class and guard it against including invalid classes. For example:

import mypackage.Item;
class mypackage.ItemList extends Object {
        public function ItemList() {
                super();
                _items = new Array();
        }
        public function get itemCount():Number {
                return _items.length;
        }
        public function addItem(item:Item):Void {
                if (!(item instanceof Item)) {
                        throw new Error("Invalid item: " + item);
                }
                _items.push(item);
        }
        public function getItem(index:Number):Item {
                var item:Item = Item(_items[index]);
                return (item == undefined) ? null : item;
        }
        public function removeItem(item:Item):Void {
                for (var i:Number = _items.length; i > 0; --i) {
                        if (_items[i - 1] == item) {
                                _items.splice(i - 1, 1);
                        }
                }
        }
        public function removeItemAt(index:Number):Void {
                _items.splice(index, 1);
        }
        public function toString():String {
                return "[type ItemList(items=" + _items.join(", ") + ")]
";
        }
        private var _items;
}

It is cumbersome to do this for all types, but it's handy for many
occasions. (I do really miss C++ templates, though....)
―
Mike Keesey
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:flashcoders-
> [EMAIL PROTECTED] On Behalf Of dc
> Sent: Saturday, September 16, 2006 7:36 PM
> To: Flashcoders mailing list
> Subject: [Flashcoders] specifying type of an array contents?
> 
> is there a way to tell flash what types an array contains?
> 
> eg given this:
> 
> var btnClipArray:Array;
> 
> --- later:
> 
> for (var btn in btnClipArray) {
>    btn.removeMovieClip();
> }
> 
> flash defaults to thiking the array contains only strings, so the
> movieclip methods throw a compile error. of course, i can cast or
> otherwise hack, but it would be nice not to...
> 
> var btnClipArray:Array:MovieClip;  ?
> 
> /dc
> -------------------------------------------
>       David "DC" Collier
> mailto:[EMAIL PROTECTED]
>       +81 (0)80 6521 9559
>       skype: callto://d3ntaku
> -------------------------------------------
>       Pikkle 株式会社
>       http://www.pikkle.com
> -------------------------------------------
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> 
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to