Search code examples
flashactionscript-3vectorcastingstrong-typing

Create a vector with a runtime defined data type


Typically you create a Vector (strongly typed array) specifying a data type like:

new Vector<PictureBox>();

However I need to create a utility method that should be able to create a vector of any given datatype. Is it possible to specify a type using a variable instead of hard-coding it?

var type:Class = PictureBox;
new Vector<type>();

Solution

  • You cannot do it exactly the way you want, but you could use a set of classes which implement the same interface, and then type your vector with that interface, e.g.:

    var list:Vector.<IBox>
    
    class PictureBox implements IBox
    class TextBox implements IBox