<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Words, punctuated &#187; Coding</title>
	<atom:link href="http://probertson.com/articles/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://probertson.com</link>
	<description>Thoughts on web development, user-centered design, code, etc. by Paul Robertson</description>
	<lastBuildDate>Tue, 20 Jul 2010 21:29:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A useful tool, the joy of shipping code, and a great developer</title>
		<link>http://probertson.com/articles/2009/03/12/useful-tool-joy-of-shipping-code/</link>
		<comments>http://probertson.com/articles/2009/03/12/useful-tool-joy-of-shipping-code/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 18:23:09 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Application Design]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[local SQL database]]></category>

		<guid isPermaLink="false">http://probertson.com/?p=240</guid>
		<description><![CDATA[If you use David Deraedt&#8217;s great AIR SQLite administration tool Lita, you probably already saw that he just pushed an update (v. 1.2) a few days ago. Aside from my general happiness from seeing that this release fixes some bugs and adds new features that were important to me, this release has personal significance for [...]]]></description>
			<content:encoded><![CDATA[<p>If you use <a href="http://www.dehats.com/">David Deraedt</a>&#8217;s great <a href="http://www.dehats.com/drupal/?q=node/58">AIR SQLite administration tool Lita</a>, you probably already saw that he just pushed an update (v. 1.2) a few days ago. Aside from my general happiness from seeing that this release fixes some bugs and adds new features that were important to me, this release has personal significance for me as well. As David <a href="http://www.dehats.com/drupal/?q=node/75">noted last week</a>, I have joined him as a contributor to the project. So in fact, some of those bug fixes and new features were done by me! I&#8217;ve discovered that there are few feelings greater than wanting a feature in a tool you use every day, and then actually implementing it. =) Upgrading is more fun when you see your own bugs in the release notes, too =)</p>
<p>As David pointed out, the fact that I&#8217;m an Adobe employee and am participating in the project doesn&#8217;t mean it&#8217;s now an official Adobe product (for good or bad). I&#8217;m doing this 100% on my own time. And my role is still pretty small &#8212; David is certainly the lead, main, primary, controlling, etc. author. I just file bugs as I find them, add some comments about feature requests, and fix issues when I know how to.</p>
<p>In case you&#8217;re curious, the back story is really pretty straightforward. I started writing my own version of an AIR SQLite admin tool back before AIR beta 1, but never had time to take it beyond a &#8220;query runner&#8221; tool. Late in 2008 I discovered Lita, and once I started using it I realized that 1) it is implemented in a similar way to many of the ideas I had, and 2) It&#8217;s already got a big head start in features, so there&#8217;s not much point in me trying to &#8220;compete&#8221; or anything like that, especially for something that I wasn&#8217;t planning to make money from.</p>
<p>After using it for a while I discovered a few bugs, and decided to email David about them. (He and I had communicated a bit previously, about the <a href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS8AFC5E35-DC79-4082-9AD4-DE1A2B41DAAF.html">encrypted database functionality added in Adobe AIR 1.5</a> and how to integrate it into Lita.) I think I probably mentioned in that message that I would be willing/interested to fix issues myself as I have time. Fortunately David was very kind and accepted my offer. So, as I said, now when I find issues I get to fix them myself, which is nice because I can fix them quickly, but also adds some responsibility since now the burden is on me to make those changes myself =)</p>
<p>As a side note, I really want to point out that David is a really great developer &#8212; something I appreciate greatly as we work in the same codebase. It is a big testament to his architectural and coding skills that I was able to dive right in and fix four bugs/feature requests in a very short time (literally a matter of minutes after first looking at the code). I&#8217;ve learned a lot just from seeing his code, and now I&#8217;m anxious to read his <a href="http://www.dehats.com/drupal/?q=node/32">&#8220;Flex Architecture Fundamentals&#8221; series</a> to learn more about the thinking behind the great code.</p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2009/03/12/useful-tool-joy-of-shipping-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New project: ActionScript Regular Expressions Testing Tool</title>
		<link>http://probertson.com/articles/2007/05/18/project-announcement-regexp-tool/</link>
		<comments>http://probertson.com/articles/2007/05/18/project-announcement-regexp-tool/#comments</comments>
		<pubDate>Fri, 18 May 2007 19:11:03 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Articles by Paul]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://probertson.com/articles/2007/05/18/project-announcement-regexp-tool/</guid>
		<description><![CDATA[Over the last couple of weeks I&#8217;ve been working on a Flex project where I&#8217;ve had to make heavy use of regular expressions. As always seems to happen when I work with regular expressions in any language, I found that one of the more complex parts of the process was just figuring out the exact [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last couple of weeks I&#8217;ve been working on a Flex project where I&#8217;ve had to make heavy use of regular expressions. As always seems to happen when I work with regular expressions in any language, I found that one of the more complex parts of the process was just figuring out the exact regular expression pattern to use.</p>
<p>Having been down this road before with other languages, I decided to put together a simple tool for testing out regular expressions. I wanted to be able to enter a pattern and a test string, and have it check to see if it was a match. That way, I could test out various possible good matches (and non-matches) to see if they give the right result. I was using named capturing groups quite a bit on this project, so I added an extra feature where it shows all the groups that are captured as well.</p>
<p>Anyway, I realized this might be useful for anyone who&#8217;s trying to use regular expressions in an ActionScript 3.0 project, so I thought I&#8217;d make it available.</p>
<p>Want to test it out? Visit the <a href="/projects/actionscript-regexp-test/">project page</a> to see the tool and download the source code.</p>
<p>If you have comments or improvements for the project, feel free to add a note in the comments on the project page.</p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2007/05/18/project-announcement-regexp-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tamarin link roundup</title>
		<link>http://probertson.com/articles/2006/11/07/tamarin-link-roundup/</link>
		<comments>http://probertson.com/articles/2006/11/07/tamarin-link-roundup/#comments</comments>
		<pubDate>Wed, 08 Nov 2006 00:09:29 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Articles to remember]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Elsewhere on the web]]></category>
		<category><![CDATA[Web Browsers]]></category>

		<guid isPermaLink="false">http://probertson.com/articles/2006/11/07/tamarin-link-roundup/</guid>
		<description><![CDATA[Some of the most useful links I&#8217;ve found on the recent project Tamarin announcement (Adobe&#8217;s contribution of code for AVM2&#8212;the ActionScript processing virtual machine portion of Flash Player&#8212;to the Mozilla Foundation).

Tamarin project page
Mozilla foundation press release
Nice executive summary and analysis by Frank Hecker, executive director of the Mozilla Foundation
Benchmark comparisons of Tamarin versus JavaScript performance

If [...]]]></description>
			<content:encoded><![CDATA[<p>Some of the most useful links I&#8217;ve found on the recent project Tamarin announcement (Adobe&#8217;s contribution of code for AVM2&#8212;the ActionScript processing virtual machine portion of Flash Player&#8212;to the Mozilla Foundation).</p>
<ul>
<li><a href="http://www.mozilla.org/projects/tamarin/">Tamarin project page</a></li>
<li><a href="http://www.mozilla.com/en-US/press/mozilla-2006-11-07.html">Mozilla foundation press release</a></li>
<li><a href="http://www.hecker.org/mozilla/adobe-mozilla-and-tamarin">Nice executive summary and analysis</a> by Frank Hecker, executive director of the Mozilla Foundation</li>
<li><a href="http://www.playercore.com/pub/Tamarin/Avmplus_vs._javascript.htm">Benchmark comparisons of Tamarin versus JavaScript performance</a></li>
</ul>
<p>If you&#8217;ve only heard vague things about this deal and want a good summary, I&#8217;d read the article by Frank Hecker linked to above.</p>
<p>Most of the obvious and publicly discussed benefits are benefits for the Mozilla Foundation, Firefox, and developers using Mozilla technologies. Personally (as someone involved in ActionScript development) I think this is a really exciting announcement. Over the next day or two I hope to post my thoughts about what this means for the Flash Platform, and why I think it&#8217;s a good thing for Adobe (and, via trickle-down effect, for Adobe&#8217;s customers). But I&#8217;m on dad duty tonight, so that&#8217;ll have to wait a bit =).</p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2006/11/07/tamarin-link-roundup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Flash as a game platform</title>
		<link>http://probertson.com/articles/2005/03/21/flash-game-platform/</link>
		<comments>http://probertson.com/articles/2005/03/21/flash-game-platform/#comments</comments>
		<pubDate>Mon, 21 Mar 2005 22:22:32 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[Articles to remember]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Elsewhere on the web]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://probertson.com/articles/2005/03/21/flash-game-platform/</guid>
		<description><![CDATA[A nice article/presentation summary about using Flash for game development. It has a nice discussion of limitations and workarounds of the Flash format for demanding applications; the resources at the end include a lot I knew and a few I am glad I found.
(via Zeh Fernando)
]]></description>
			<content:encoded><![CDATA[<p>A nice article/presentation summary about <a href="http://www.drizzle.com/~scottb/gdc/flash-paper.htm">using Flash for game development</a>. It has a nice discussion of limitations and workarounds of the Flash format for demanding applications; the resources at the end include a lot I knew and a few I am glad I found.</p>
<p>(via <a href="http://newsfeed.fatorcaos.com.br/">Zeh Fernando</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2005/03/21/flash-game-platform/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting type checking with ActionScript arrays</title>
		<link>http://probertson.com/articles/2005/03/17/strongly-typed-arrays/</link>
		<comments>http://probertson.com/articles/2005/03/17/strongly-typed-arrays/#comments</comments>
		<pubDate>Thu, 17 Mar 2005 12:21:13 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Articles by Paul]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://probertson.com/articles/2005/03/17/actionscript-arrays-and-strong-typing/</guid>
		<description><![CDATA[In ActionScript, unlike other languages, arrays can contain a mix of different data types. This leads to potential confusion and errors because ActionScript compilers don't check that data of a certain type is being added/retrieved from the array. This article discusses this issue, and several solutions as well.]]></description>
			<content:encoded><![CDATA[<p>Yesterday on the <a href="http://mtasc.org/">MTASC</a> list, <a href="http://blog.v-i-a.net/">erixtekila</a> asked a question about whether there was a way in ActionScript 2 to check the type of a &#8220;list item&#8221; such as an element of an array or an associative array.</p>
<p>He gave an example of what he wanted to do, from his message:</p>
<blockquote><p>
var aList [0]:MyClass; // Gives error.<br />
var aAssos [&#8220;index&#8221;]:MyClass // same<br />
var aAssos [&#8220;index&#8221;]:MyClass = new MyClass(); // isn&#8217;t permitted<br />
because you&#8217;ve got to initialize variables before assignate value,<br />
indeed with mtasc.
</p></blockquote>
<p>Essentially (if I understand correctly) he wants to be able to specify a type for elements of the array or associative array, and have the compiler check that only instances of that type are added/retrieved from the array. Unfortunately this doesn&#8217;t work the way you might expect it to if you are coming from a strongly-typed language like Java/C++/C# etc.</p>
<p>(It&#8217;s also possible that he just wants to specify the type of the data and put it into the array in one step, in which case you can just use the third line but without the <code>:MyClass</code>, or else you can initialize the instance as a separate variable and then assign that variable to the array.)</p>
<h2>Background</h2>
<p>First a little background. In ActionScript, there are two built-in types that are commonly used to hold a &#8220;collection&#8221; or array of data.</p>
<p>The Array type is like arrays in most languages &#8212; items are accessed using an integer index. However, unlike most strongly typed languages, the Array type in ActionScript allows you to store elements of any type in the array:</p>
<pre><code>var aList:Array = new Array();
aList[0] = 5; // assigning a Number
aList[1] = "hi there!" // assigning a String</code></pre>
<p class="editornote">Note: Since this article was originally posted I have made some changes to the next several paragraphs to help clarify these examples, based on some <a href="http://lists.motion-twin.com/archives/mtasc/2005-March/001362.html">helpful feedback on the MTASC list</a> from <a href="http://ncannasse.free.fr/">Nicholas Cannasse</a> (the author of MTASC).</p>
<p>ActionScript doesn&#8217;t have a specific &#8220;associative&#8221; array type, one where you use a string value as the key rather than an integer.  However, in ActionScript the basic Object type behaves as an associative array, so the most common approach to creating an associative array is just to use an Object. You can still use the square bracket array access operator &#8220;[]&#8221; to access the elements of the &#8220;array.&#8221; In actuality you are just using it for dynamic property access rather than as the array access operator<a class="footnote" href="#note1">note 1</a> &#8212; but the end result is the same.  Like with the Array, you can store any type of object as an element of your associative array Object.</p>
<p>So a general rule of thumb is that in ActionScript any time you use the <code>[]</code> operator to retrieve a value, the return type is Object. If you want to have stricter type-checking for values retrieved from an Array, you need to cast the return value to the expected type if you want to get type checking for that variable:</p>
<pre><code>var myArray:Array = new Array();
var someObj:MyType = new MyType();
myArray[0] = someObj;
var someVal:String = myArray[0].methodReturnsNumber(); // return type not checked
var otherVal:String = MyType(myArray[0]).methodReturnsNumber(); // return type checked
var someVal2:Number = myArray[0].nonExistentMethod(); // doesn't check whether method exists
var otherVal2:Number = MyType(myArray[0]).nonExistentMethod(); // checks whether method exists
</code></pre>
<p>In this example, the lines where the Array element is cast to MyType (5 and 7) will raise an error (in MTASC or the Flash MX 2004 compiler), but the other lines will fail silently. In fact, as Colin Moock points out in Essential ActionScript 2.0, even by casting the value you are only getting minimal type-checking. When you cast a variable in ActionScript, the compiler (and the Flash Player) blindly trust that the cast is correct.  For example, look at this code:</p>
<pre><code>var myArray:Array = new Array();
var someString:String = new String("asdf");
myArray[0] = someString;
var someVal:MyType = MyType(myArray[0]).methodReturnsNumber();
trace("someVal = " + someVal.toString()); // prints "someVal = undefined"
</code></pre>
<p>The compiler does not check that the object being retrieved from the Array is actually an instance of whatever type you are casting it to, and the improper cast does not cause an error at runtime either<a class="footnote" href="#note2">note 2</a>. (Unfortunately there are very few things that cause runtime errors in the Flash Player &#8212; in almost all cases things just fail silently.)</p>
<p>The big downside to this for anyone who is used to working in a language with typed arrays is that you can&#8217;t easily get compile-time type safety in ActionScript with Array (or Object used as an associative array). There is no way to designate that an array should only hold values of one type, and have the compiler give an error if values of other types are added or if retrieved values are assigned to variables of the wrong type.</p>
<h2>Solution 1: Basic typed collections</h2>
<p>The easiest solution is to create your own typed collection that has strong typing for adding and retrieving elements, that just wraps a standard Array for storing the data.</p>
<p>A simple collection class that only allowed MyType instances to be added/retrieved would look something like this:</p>
<pre><code>class MyTypeCollection
{
	private var _innerArray:Array;

	public function MyTypeCollection()
	{
		_innerArray = new Array();
	}

	public function addItem(item:MyType):Void
	{
		_innerArray.push(item);
	}

	public function getItemAt(index:Number):MyType
	{
		return MyType(_innerArray[index]);
	}
}</code></pre>
<p>There is a bit of awkwardness with this because you have to use <code>addItem()</code> and <code>getItemAt()</code> to add or access a particular item. Unfortunately as far as I have been able to dig up there isn&#8217;t a way to be able to use the [] operator to access the elements of your array. Sorry, no operator overloading in ActionScript 2 =(. But with a good editor with code hints, the extra typing is minimal, and to me the extra type checking is worth the trouble.</p>
<p>Of course, this only allows you to add and retrieve values from the collection. If you want to remove items, you need to add another method; if you want to find out how many elements are in your collection you need to add that too; pretty soon it gets to be a lot of code.</p>
<h2>Solution 2: Heavy-duty typed collections</h2>
<p>If you find yourself wanting to do this a lot (like I seem to), you may find it helpful to have a base class for a typed array that already has most of the functionality built in. Then to create your strongly typed collection class, you only need to write the methods that actually require strong typing, and just inherit the rest from the base class.</p>
<p>In all their kindness, Macromedia has written just such a class for us to use, the <code>mx.utils.CollectionImpl</code> class. Now before you tell me I&#8217;m off my rocker and there is no such class in the mx.utils package, read <a href="#note3">this footnote</a>.</p>
<p>To create your own CollectionImpl subclass, you need to override the methods that actually use your specific type either as a parameter or as the return type. That means your subclass has to include these five methods:</p>
<ul>
<li><code>addItem()</code></li>
<li><code>contains()</code></li>
<li><code>getItemAt()</code></li>
<li><code>getIterator()</code></li>
<li><code>removeItem()</code></li>
</ul>
<p>Okay, so maybe this sounds like more work than we bargained for. But it&#8217;s really not as bad as it sounds. Take a look at this <a href="/resources/2005/03/17/MyTypeCollection.as">sample CollectionImpl subclass</a> to see a real example of what you would have to include in your subclass.</p>
<p>You also need to create your own subclass of <code>mx.utils.IteratorImpl</code>, to use in the <code>getIterator()</code> method. In your <code>IteratorImpl</code> subclass you will need to override the constructor and the <code>hasNext()</code> method. You can take a look at this <a href="/resources/2005/03/17/MyTypeIterator.as">sample IteratorImpl subclass</a> to see what it needs to have.</p>
<p>If you save a copy of these sample files, all you really need to do to create your own typed collection is do a find/replace in those two files, replacing the text &#8220;MyType&#8221; with the name of the class that you want to be stored by this collection.</p>
<h2>Solution 3: Somewhere between basic and heavy-duty</h2>
<p>If you don&#8217;t want to do quite so much work, I have created an alternative base class for collections that only has three methods you need to overload &#8212; <code>addItem()</code>, <code>contains()</code>, and <code>getItemAt()</code>. It doesn&#8217;t use/require an Iterator, so you don&#8217;t have to create an <code>IteratorImpl</code> subclass either. If you are interested, here is the <a href="/resources/2005/03/17/TypedCollectionBase.as">source for the TypedCollectionBase class</a>. A sample of what a subclass would look like is in this <a href="/resources/2005/03/17/MyTypeCollection2.as">sample TypedCollectionBase subclass</a>.</p>
<h2>Solution 4: AS2LIB&#8217;s TypedArray (Extra-heavy-duty)</h2>
<p>Finally, (as Pavel Simek suggested on the MTASC list) if you want to jump in with both feet, the <a href="http://as2lib.org/">AS2LIB ActionScript framework</a> includes a TypedArray class which handles a lot of this checking as well. From a casual glance, it appears that this type checking is done at runtime rather than compile time &#8212; but I could easily be wrong on that one, as I have not actually tried it out.</p>
<p>So there you have it &#8212; a variety of options for working around the type freedom that is inherent in ActionScript&#8217;s built-in Array class, leading to lovely compile-time type checking &#8212; which I will tell you resoundingly, makes it so much easier to debug your ActionScript it is incredible.</p>
<h2>Notes</h2>
<p class="footnote">After all, it wouldn&#8217;t be me if I didn&#8217;t have more to say&#8230;=)</p>
<h3 id="note1" class="footnote">Note 1: Using Object as an associative array</h3>
<p class="footnote">The truth of the matter is that when you use an Object as an associative array, you are really just using the [] as a dynamic property accessor rather than as an array accessor. Consider this example:</p>
<pre><code>var myAssoc:Object = new Object();
myAssoc["index"] = 7;
myAssoc.index2 = "hi there!";
trace(myAssoc["index"]);  // prints out 7
trace(myAssoc.index); // prints out 7
trace(myAssoc["index2"]); // prints out "hi there!"
trace(myAssoc.index2); // prints out "hi there!"</code></pre>
<p class="footnote">The &#8220;element&#8221; is really just dynamically added as a property on our Object instance, and can be accessed just the same using the dot operator as using the [] operator. This works for any class that is declared as a &#8220;dynamic&#8221; class &#8212; properties can be dynamically added and removed from instances using the [] operator or the dot operator.</p>
<p class="footnote">In fact, I believe that in the underlying guts that is how the Array type works, too (note that Array is defined as a dynamic class). When you add an element to an Array with an integer index, behind the scenes a new property is created on the Array with the name of that integer index. Of course, you will get a syntax error if you try to access the property using dot syntax (e.g. myArray.0).  However, when you use the debugger in Flash you can see that new properties &#8220;appear&#8221; when they are added. The screenshots in the sequence below show how the properties are added to the Array as we step through the code.</p>
<p class="caption">An Array &#8220;myArray has been defined and two elements [0] and [1] have been added; notice they appear in the tree just the same as any other object property in the Flash debugger.</p>
<p><img src="/resources/2005/03/17/debugger3.png" width="562" height="338" alt="Debugger Step 2" /></p>
<p class="caption">Two properties, &#8220;index&#8221; and &#8220;index2&#8221;, have been added to the array using the dynamic property access operator []; notice that the debugger doesn&#8217;t make a distinction between the named properties and the numbered array elements.</p>
<p><img src="/resources/2005/03/17/debugger5.png" width="562" height="338" alt="Debugger Step 5" /></p>
<h3 id="note2" class="footnote">Note 2: Type checking at runtime</h3>
<p class="footnote">Fortunately, there is a way that you can check the type of a variable at runtime, and if you aren&#8217;t going to create your own strongly typed collection class I suggest you use this approach instead. ActionScript has an <code>instanceof</code> operator that can be used to check the type of an object. The best way to use this with array elements is to assign the retrieved value to a variable of type Object, then test that object using instanceof:</p>
<pre><code>var myArray:Array = new Array();
var someObj:MyType = new MyType();
myArray[0] = someObj;
var someOtherObj:OtherType = new OtherType();
myArray[1] = someOtherObj;
var someVal1:MyType = myArray[0]; // the correct type
var someVal2:MyType = myArray[1]; // not the correct type
var someVal3:OtherType = OtherType(myArray[1]);
var someVal4:Object = myArray[0];
var someVal5:Object = myArray[1];
var test1:Boolean = someVal1 instanceof MyType; // true
var test2:Boolean = someVal2 instanceof OtherType; // true, but should be false
var test3:Boolean = someVal3 instanceof OtherType; // true
var test4:Boolean = someVal4 instanceof MyType; // true
var test5:Boolean = someVal4 instanceof OtherType; // false
var test6:Boolean = someVal5 instanceof OtherType; // true</code></pre>
<p class="footnote">Test 2 fails because the retrieved value is assigned to a variable of the wrong type, and the silently failing cast has already taken place. Compare this to test 5, where the retrieved value was assigned to a variable of type Object. In that case instanceof returned the correct value. Moral of the story: if you&#8217;re going to use instanceof, make sure your &#8220;test&#8221; variable is typed as Object. If instanceof succeeds you can then cast the value and use it as the correct type, like this:</p>
<pre><code>var myArray:Array = new Array();
var someObj:MyType = new MyType();
myArray[0] = someObj;
.
.
.
var testVar:Object = myArray[0];
if (testVar instanceof MyType)
{
	var typedVar:MyType = MyType(testVar);
	// do something with typedVar
}</code></pre>
<p class="footnote">Of course, be sure to read the <a href="http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001371.html">caveat about instanceof and instrinsic types (Boolean, Number, and String) in the documentation</a>. Essentially, instanceof will return inconsistent results for those types depending on how their original value was assigned. For complete checking of those types, you need to check them using instanceof and the older <a href="http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001865.html">typeof</a> also, making your code look like this:</p>
<pre><code>var myArray:Array = new Array();
var someString:String = "asdf";
myArray[0] = someString;
.
.
.
var testVar:Object = myArray[0];
if (testVar instanceof String || typeof(testVar) == "string")
{
	var typedVar:String = String(testVar);
	// do something with typedVar
}</code></pre>
<h3 id="note3" class="footnote">Note 3: getting the mx.utils.CollectionImpl class</h3>
<p class="footnote">The mx.utils.CollectionImpl class doesn&#8217;t come with Flash; instead, it is included in the source code for the Flash Remoting Components, which anyone can download for free from the <a href="http://www.macromedia.com/software/flashremoting/downloads/components/">Flash Remoting Components page</a>. The link is a little hard to find &#8212; there are several links to download the MX 2004/AS2 components in various languages, and then down at the bottom of that section there is a small section titled &#8220;Source Code for Macromedia Flash Remoting for Flash MX 2004 ActionScript 2.0&#8221;. Under that heading you will find a download, which contains the ActionScript class source code (you don&#8217;t actually get the source by downloading the components, which is a puzzle to me).</p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2005/03/17/strongly-typed-arrays/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes (C# 2.0)</title>
		<link>http://probertson.com/articles/2004/12/05/c-sharp-2-features/</link>
		<comments>http://probertson.com/articles/2004/12/05/c-sharp-2-features/#comments</comments>
		<pubDate>Sun, 05 Dec 2004 16:22:15 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[Articles to remember]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://probertson.com/articles/2004/12/05/c-sharp-2-features/</guid>
		<description><![CDATA[A nice look at the new features in C# 2.0
Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes (C# 2.0)
]]></description>
			<content:encoded><![CDATA[<p>A nice look at the new features in C# 2.0</p>
<p><a href="http://msdn.microsoft.com/msdnmag/issues/04/05/C20/default.aspx">Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes (C# 2.0)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2004/12/05/c-sharp-2-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSS 2.0 Specification</title>
		<link>http://probertson.com/articles/2003/10/07/rss-20-specification/</link>
		<comments>http://probertson.com/articles/2003/10/07/rss-20-specification/#comments</comments>
		<pubDate>Tue, 07 Oct 2003 09:08:35 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[Articles to remember]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://probertson.com/articles/2003/10/07/rss-20-specification/</guid>
		<description><![CDATA[RSS 2.0 Specification
]]></description>
			<content:encoded><![CDATA[<p><a href="http://blogs.law.harvard.edu/tech/rss">RSS 2.0 Specification</a></p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2003/10/07/rss-20-specification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SOAP meets RSS</title>
		<link>http://probertson.com/articles/2003/10/07/soap-meets-rss/</link>
		<comments>http://probertson.com/articles/2003/10/07/soap-meets-rss/#comments</comments>
		<pubDate>Tue, 07 Oct 2003 09:06:04 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[Articles to remember]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://probertson.com/articles/2003/10/07/soap-meets-rss/</guid>
		<description><![CDATA[SOAP meets RSS
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.thetwowayweb.com/soapmeetsrss">SOAP meets RSS</a></p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2003/10/07/soap-meets-rss/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a generic RSS tool</title>
		<link>http://probertson.com/articles/2003/09/29/creating-a-generic-rss-tool/</link>
		<comments>http://probertson.com/articles/2003/09/29/creating-a-generic-rss-tool/#comments</comments>
		<pubDate>Mon, 29 Sep 2003 14:23:04 +0000</pubDate>
		<dc:creator>Paul Robertson</dc:creator>
				<category><![CDATA[Articles to remember]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://probertson.com/articles/2003/09/29/creating-a-generic-rss-tool/</guid>
		<description><![CDATA[Creating a generic RSS tool
]]></description>
			<content:encoded><![CDATA[<p><a href="http://weblogs.asp.net/rosherove/articles/29509.aspx">Creating a generic RSS tool</a></p>
]]></content:encoded>
			<wfw:commentRss>http://probertson.com/articles/2003/09/29/creating-a-generic-rss-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
