I’m looking for method to create Vector and push some values without defining variable Vector. For example:
I have function:
public function bla(data:Vector.<Object>):void { ... }
this function expects Vector as parameter. I can pass parameters this way
var newVector:Vector.<Object> = new Vector.<Object>();
newVector.push("bla1");
newVector.push("bla2");
bla(newVector);
Can I do it in one line in Flex? I’m looking for something like:
bla(new Vector.<Object>().push("bla1").push("bla2"));
I’ve also tried this:
bla(function():Vector.<Object> { var result:Vector.<Object> = new Vector.<Object>(2, true); result.push("bla1"); result.push("bla2"); return result; });
But it complains:
1067: Implicit coercion of a value of type Function to an unrelated type __AS3__.vec:Vector.<Object>...
Thanks
You can’t chain
Vector.push()calls as they returnuint‘s — the new vector length.The coercion problem, on the other hand, happens because you are passing a function to the
blafunction, which expects aVector.<Object>.You could fix that easily:
However, there’s already a top level function in AS3 that helps you creating vectors.
The
Vector()function expects either anArrayor aVectorand returns aVector. So, for example, you could use:Visit the AS3 Reference for more info.
EDIT: I forgot to mention that the fix on the function approach was simply adding a
()to it, meaning we actually called the anonymous function and passed it’s return to theblafunction.