Tag Archives: quick tip

Quick Tip – Return Vector Array


If you try to return a Vector typed array in flash, you’ll get the following error.

public function getMainItems():Vector
{
	//Length of Main items
	MainItemLn = xml.item.length();

	//Vector class of MainItems
	var MainVO:Vector     = new Vector.<VOItem>(MainItemLn,true);

	for(var i:int = 0; i < MainItemLn; ++i)
	{
		MainVO[i]        = new VOItem();
		MainVO[i].id     = i;
		MainVO[i].title  = xml.item[i].@title;
		MainVO[i].img = xml.item[i].@img;
	}

	return MainVO
}

1067: Implicit coercion of a value of type __AS3__.vec:Vector.<com.Gallery:VOItem> to an unrelated type __AS3__.vec:Vector.

It is because Vector.<VOItem> isn’t the same type as Vector. That means;

public function getMainItems():Vector.<VOItem>
{
	//Length of Main items
	MainItemLn = xml.item.length();

	//Vector class of MainItems
	var MainVO:Vector     = new Vector.<VOItem>(MainItemLn,true);

	for(var i:int = 0; i < MainItemLn; ++i)
	{
		MainVO[i]        = new VOItem();
		MainVO[i].id     = i;
		MainVO[i].title  = xml.item[i].@title;
		MainVO[i].img = xml.item[i].@img;
	}

	return MainVO
}

And you can get your Vector array values like;

trace(getMainItems()[0].title);
//or
trace(getMainItems()[1].title);
//and so on..
.
.
.

**Note: VOItem is my custom Value Object Class. It doesn’t matter what it is. It can be movieclip, sprite, etc…