7 Replies Latest reply on Nov 1, 2007 10:11 AM by liekeroman

    Custom Array in ActionScript

    liekeroman
      How can I use custom array objects in my ActionScript classes?

      For Example:

      public class Person
      {
      public var id:int;
      public var name:String;
      public var address:Address[]
      }

      where Address is another class which holds all the detailed elements of address.

      When I try this code I'm getting a Syntax Error exception in my ActionScript, how can I use my own Array of objects?

      Kind Regards,
      Nathalie
        • 1. Re: Custom Array in ActionScript
          peterent Level 2
          Use either:

          public var address:Array;
          or
          public var address:Array = new Array(); // to initialize it to empty array
          or
          public function Person() {
          address = new Array(); // to initialize it in the constructor
          }
          • 2. Re: Custom Array in ActionScript
            liekeroman Level 1
            Hi Peter,

            Thanks for your reply, but I think I haven't explained my problem correctly to you.
            The problem I'm facing isn't about initializing arrays but making sure of strongly typed arrays.
            The Actionscript objects I'm defining need to be mapped to java objects used in my web service proxy client, which means the actionscript-objects' signature needs to be identical to my java classes.

            My java-class, used by my web service proxy, looks like the following:
            public class Person {
            protected Integer id;
            protected String name;
            protected Address[] address
            }

            The ActionScript class will need to use an array of Address-objects instead of a regular Array. My question is, how can I define the address-variable as being an array of Address-elements?

            Kind regards,
            Nathalie
            • 3. Re: Custom Array in ActionScript
              peterent Level 2
              My Java and C++ days are long gone by. I see what you want. Check out the [ArrayElementType]
              metadata. That's about as close as you'll get in ActionScript.

              The language doesn't support typed arrays.
              • 4. Re: Custom Array in ActionScript
                liekeroman Level 1
                Hi Peter,

                As I understand correctly from the documentation ArrayElementType is only checked at run-time and when you're using classes as type-value, you need to define the fully-qualified name.

                This can lead to lots of errors or inconsistencies when changing package-structure, signature, ...

                Isn't there a library made available by adobe for this common problems or enhancement packages in ActionScript 3.0?

                Tutorials or best practices would be very usefull here.

                Kind regards,
                Nathalie
                • 5. Re: Custom Array in ActionScript
                  peterent Level 2
                  The issue is with the language and the standard (ECMAScript). There is no provision for typed arrays. So we don't have anything to help with this.
                  • 6. Re: Custom Array in ActionScript
                    atta707 Level 2
                    one way of doing it right from the flex builder help:

                    public dynamic class TypedArray extends Array
                    {
                    private var dataType:Class;
                    public function TypedArray(typeParam:Class, ...args)
                    {
                    dataType = typeParam;
                    var n:uint = args.length
                    if (n == 1 && (args[0] is Number))
                    {
                    var dlen:Number = args[0];
                    var ulen:uint = dlen
                    if (ulen != dlen)
                    {
                    throw new RangeError("Array index is not a 32-bit unsigned integer ("+dlen+")")
                    }
                    length = ulen;
                    }
                    else
                    {
                    for (var i:int=0; i < n; i++)
                    {
                    // type check done in push()
                    this.push(args)
                    }
                    length = this.length;
                    }
                    }
                    }
                    • 7. Re: Custom Array in ActionScript
                      liekeroman Level 1
                      I've extended the ActionScript objects to use TypedCollection to enable typed objects, but now I'm facing another exception regarding serialization of my remote java classes.

                      The following url http://www.nabble.com/DataService-Serialization-of-ArrayCollection-subclass-t4108567.html, clearly states the issue, but I haven't been able to resolve the issue by implementing the Externalizable interface in my java classes. DataService serialization of ArrayCollection subclass