Reputation: 3165
In a flex project, I have created kind of abstract class named AbstractAlert
and some subclass of AbstractAlert
named Alert1
, Alert2
...
I manipulate Vector
of type Alert1
, Alert2
...like this :
var alerts1:Vector.<Alert1> = new Vector.<Alert1>();
var alerts2:Vector.<Alert2> = new Vector.<Alert2>();
How can I han handle generic function to manipulate these vectors ?
I can't call this function :
private function someFunction(from:Vector.<AbstractAlerte>):void
{
...
}
someFunction(alerts1);
Is there a way to make generic function ?
Upvotes: 0
Views: 98
Reputation:
Edit: This section isn't correct
Concerning your second comment, yes, you won't modify the original object if it is an Vector (iirc? Same goes for Array as well, i think). This isn't because of the cast, but because passed arguments of Array and Vector types are not treated a references.
If you want to modify the passed Vector, you could do the following:
var alerts1:Vector<Alert1> = new Vector.<Alert1>();
...fill with objects
alerts1[0].property = "hey";
trace(alerts1[0].property); //output : "hey"
alerts1 = Vector.<Alert1>(someFunction(Vector.<AbstractAlerte>(alerts1)));
trace(alerts1[0].property); //output : "sup"
private function someFunction(from:Vector.<AbstractAlerte>):Vector.<AbstractAlerte>
{
from[0].property = "sup";
return from;
}
But this seems a bit cumbersome, don't you think? With an Interface that all Alert Classes implement, you could forego all this casting back and forth.
Lets say you have an Interface called IAlert. Instead of writing
var alerts1:Vector<Alert1> = new Vector.<Alert1>();
you could write
var alerts1:Vector<IAlert> = new Vector.<IAlert>();
and fill it with Objects of type Alert1. Give IAlert a function called
public function get type():String
Include a variable to hold the type of the class in AbstractAlert, and set the type in the constructors of Alert1 and Alert2, like this in essence:
public interface IAlert{
function get type():String;
}
public class AbstractAlert implements IAlert{
private var _type:String;
public function AbstractAlert(type:String){
this._type = type;
}
public function get type():String{return "AbstractAlert";}
}
public class Alert1 extends AbstractAlert implements IAlert{
public function Alert1(){
super("Alert1");
}
public function get type():String {return super.type;}
}
then, finally in your someFunction
function:
private function someFunction(from:Vector.<IAlert>):Vector.<IAlert>{
if(from{0].type == "Alert1"){
//do stuff with Alert1 classes
}
etc.
return from;
}
This is untested code, but I hope you get the gist of what I'm saying.
Upvotes: 1
Reputation: 3165
DodgerThud, I'm pretty sure that your assertion :
This isn't because of the cast, but because passed arguments of Array and Vector types are not treated a references
is false because I tested this code :
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
creationComplete="onCreationComplete(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private var _vector:Vector.<Alert1> = new Vector.<Alert1>();
protected function onCreationComplete(event:FlexEvent):void
{
// Alert1 is extending AbstractAlert
_vector.push(new Alert1());
_vector.push(new Alert1());
trace(_vector.length);
modify(Vector.<AbstractAlert>(_vector));
trace(_vector.length);
modify2(_vector);
trace(_vector.length);
}
protected function modify(vector:Vector.<AbstractAlert>):void
{
vector.splice(0, 1);
}
protected function modify2(vector:Vector.<Alert1>):void
{
vector.splice(0, 1);
}
]]>
</fx:Script>
And result is :
2 2 1
So we can pass vectors by reference (I believe that only primitives types are passed by values).
Upvotes: 0