-
Arrays in classes
I've just started experimenting with classes, so I'm guessing my problem is pretty basic. What I am doing is passing an array into a method of a class. The array is full of strings. I take each section of the array and pass that to another method which returns an object, and I replace the string that I passed with the object. When I have gone through the entire array, I then return the array to the initial array that I used as a argument to the method. The problem is, that the final array is just like the initial array. Nothing changes. Anybody know why?
-
I think the problem is somewhere around here...
"another method which returns an object, and I replace the string that I passed with the object"
Just changing the string variable might not be changing the contents to the array. (Although it's hard to tell from just a prose description of your code). I think what you need to do is...
Code:
var A : Array = ["truck", "car", "van"]
for (var i : int = 0; i < A.length; i++)
{
var s : String = A[i];
var obj : Object = AutomobileFactory.create(s);
A[i] = obj;
}
What definitely won't work is:
Code:
var A : Array = ["truck", "car", "van"]
for (var i : int = 0; i < A.length; i++)
{
var s : String = A[i];
var obj : Object = AutomobileFactory.create(s);
s = obj;
}
In my personal opinion, a better policy is to return a new Array of objects without clobbering the original input Array. I highly doubt this will have any negative effect on performance...
Code:
var A : Array = ["truck", "car", "van"];
var Aprime : Array = CarDealership.fulfillOrder(A);
public function CarDealership.fulfillOrder(order : Array) : Array
{
var returnvalue : Array = new Array;
for (var i : int = 0; i < order.length; i++)
returnvalue.push( AutomobileFactor.create(order[i]) );
return returnvalue;
}
public function AutomobileFactory.create(s : String) : Object
{
switch s:
{
case "car" : return new Car();
case "van" : return new Van();
case "truck" : return new Truck();
}
}
I'm just making up an example off the cuff, but the basic idea is that a user defines an order of automobles, which is just an Array of strings describing what s|he wants. The CarDealership returns an Array of Objects, where each is a new Object, freshly created() by an AutomobileFactory. It is possible that in your application, CarDealership and AutomobileFactory could be combined into one class, but your description made it sound like creating new objects is delegated to another class. So I did that too.
I think posting your source code that demonstrates the problem is worth 1000 words in this case.
-
Alright thanks for your reply. Let me try to explain my problem a little bit better.
I have an array
[CODE]
t = ["a","b","c","d","e"];
t = method1(t);
public static function method1(t2:Array):Array {
for(var z = 0;z<t2.length;z++)
{
t2[z] = method2(firstNode,t2[z]);
}
}
-
That compiles? It specifies a return type and never returns a value. (AS3 nono IIRC, maybe as2 doesn't complain). What is firstNode?
EDIT: Still not clicking a lightbulb to me. The second trace statement should throw an access violation (it's reading beyond the end of the array, z = t2.length, and a[z] is undefined). I would really encourage making a new array inside method1() and returning that instead of the input argument. Some sort of aliasing is going on there that isn't clear to me (to be honest, I am not sure of the passing semantics when passing an Array. My guess is that you get a copy of the reference, but changes done to the array internals are still visible to the caller).
-
Alright thanks for your reply. Let me try to explain my problem a little bit better.
I have an array
Code:
t = ["a","b","c","d","e"];
t = method1(t);
public static function method1(t2:Array):Array {
for(var z = 0;z<t2.length;z++)
{
t2[z] = method2(firstNode,t2[z]);
trace(t2[z]); //outpus [object] [object]
}
}
trace(t2[z]); //outputs "a"
return t2;
//the returned t2 has not changed from the original array that was passed to it.
}
private static function method2(x:XMLNODE, u:String):Object {
//do a bunch of things
var u:MyClass = new MyClass(u);
return u;
}
Hopefully this helps...
-
A private static method?? Erm that sort of defeats the purpose doesn't it?