12 Replies Latest reply on Jan 6, 2007 4:34 PM by Peter Lorent

    Setting data type of an object at run time

    sl0beck
      This is something of an advanced problem, so I hope there's someone out there who can speak to this. I apologize for the length of the post, I'm just trying to be clear about a rather unclear conundrum. --

      We all use arrays to hold collections of things. As you know, an array is its self a type (Array) and so there is no way to declare, for instance, an array of MovieClips or an array of Strings. Heres what happens: lets pretend that we have 3 movie clips clip1_mc, clip2_mc, and clip3_mc, and an Array called anArray. so we can legally do this. anArray.push( clip1_mc ); anArray.push( clip2_mc ); anArray.push( clip3_mc );. An array of size 3. Now the code: trace( typeof anArray[0] ); will output "object." This can be a problem. If you try this: var newClip:MovieClip = anArray[0]; you get a type mismatch from the compiler. "Found Object where MovieClip is required." It necessitates this: var newClip:MovieClip = MovieClip( anArray[0] ); Bummer no?

      Ok. So fast forward. Lets assume a class List that uses an array to hold the items in the list. So the class uses the Array class in "composition." So you have methods like insert( ); and retrieve(). Retriece() returns an item from an index in the array. Because of the aforementioned problem, no matter what type the thing was going into the list it comes out with type Object because the retrieve function must return an item of some type, the only option is to declare the return type of the retrieve() function as Object. Lets say you know that this is a list of Strings. You could write the retrieve()'s code like: return String( returnItem ); And type the object "on the way out" .. but what if you want the list to work with any type object, you would have to have a variable to hold the desired type of objects in the list. And then as the objects are being returned, you'd cast them to the type stored in that variable. I guess that you'd pass the type to the constructor of the List: var aList:List = new List( "AddressCard" ); -- That should declare a List (Array) of AddressCards.

      Is there a way to do this? have the type specified in casting be dynamic?

      And the flip-side of that is detecting the name of the class of an object instance. The thing here would be to try to derive a String from the name of the constructor function. (which is always the class name) In actionscript 3 there is a service package-- mx.util -- I think, that has a function to return the qualified class name of an object. But is there a jimmy rig for this in AS2?

      My actual project does not use an Array to implement List. ( or Stack ) It uses a structure called a "linked list" - which is a whole other topic, but same problem as with Arrays is also present in the linked list versions and so requires the same solution. If one is willing to type things explicitly when assigning an item in the list to a variable of some type, this thing is quite excellent. But I don't like it. I have the package with docs, code, sample fla's, diagrams and everything that I would encourage people to download, look at, try and whatever and then come back here with any thoughts on it. There is a component with Flash professional that is a list like structure, but it's so general that it crosses the line into bloat. Ie: if you are using a list as a stack, you only need getTop() and pop() and push() not all the other (12 or so) methods in the class. Mine are patterned after structures from the C++ standard template library. And the code style is C++, but I think it's actually easier to read that way.

      the files: http://www.null.bz/ADL_Lib/ADT_Lib.zip

      Thanks in advance, for your efforts! I really appreciate all of it, helpful or not.