Newbie question here: I’m working with Flash Builder 4.5 on an Actionscript project. I’ve created the following classes:
package
{
public class ComplexNumber
{
public var real:Number; // real component
public var imag:Number; // imaginary component
}
}
and,
package
{
public class ComplexArray
{
public var real:Array; // real component array
public var imag:Array; // imaginary component array
}
}
and a static function:
package
{
public class ComplexDivide
{
public static function v1p0(a:Number, b:Number, // numerator: a+bi
c:Number, d:Number // denominator: c+di
):ComplexNumber
{
var z:ComplexNumber = new ComplexNumber();
var divisor:Number = c*c + d*d;
z.real = (a*c + b*d) / divisor; // real component
z.imag = (b*c - a*d) / divisor; // imaginary component
return z;
}
}
}
and in another .as file I’m trying to call this function
var BXFN_complex:ComplexArray = new ComplexArray();
for (var ii:int = 0; ii <= 2; ii++) {
BXFN_complex[ii] = ComplexDivide.v1p0( 1, 0, 2, 3 );
}
but the code inside this loop generates the following run time error: “ReferenceError: Error #1056: Cannot create property 0 on ComplexArray.” Thus, my code for “BXFN_complex[ii] = ~” is incorrect. Anyone know how to achieve what I’m trying to do? Basically, ComplexDivide.v1p0 returns two numbers, and BXFN_complex is an object containing two number arrays, and I want to assign the ComplexDivide two numbers into the ii’th element of arrays in BXFN_complex.
I am not sure I understand what you are trying to accomplish, and frankly I find your code hard to read, but the cause of the error is simple:
Your ComplexArray instance does not behave like an Array – it is an Object with two member Arrays, real and imag, and you need to specify which one you are acually addressing:
or
instead of just
If you want to pass in just a ComplexNumber type, you can use a function to do it:
Don’t forget to initialize the real and imag arrays before adding items!
Last but not least I don’t understand why you don’t use a primitive array which holds ComplexNumbers, instead of a ComplexArray which holds two arrays of primitive Number values – then you could just use
BXFN_complex[ii]like you did before.