Tuesday, November 4, 2008

FlexBuilder and XML

This is legal Flex syntax:

function foo() : void
{
var bar : XML = <mytag attr1="1"/>;
}

function bar() : void
{
}

However, the fact that your XML variable has an implicit closing tag instead of an explicit one will confuse poor FlexBuilder's brain, so that it doesn't detect bar (or, in fact, anything after foo) as a function. Symptoms include:

  1. Functions after foo won't show up in the Outline View

  2. FlexBuilder won't let you use F3 ("Goto Definition") to go to anything in that class


The solution is to put in a closing tag:
function foo() : void
{
var bar : XML = <mytag attr1="1"></mytag>;
}

function bar() : void
{
}

Flex VerifyError #1031: Scope depth is unbalanced

Flex doesn't like returns from blocks nested in switch/case statements. For example, if you do this:

public function foo(thing : String, cond : Boolean) : void
{
switch (thing)
{
case "foo":
if (cond)
{
return "Hello";
}
return "World";
case "bar":
// stuff
}
}


You'll get a VerifyError if you hit the return "Hello" line.

The solution is to set a return variable and do your return from the case, rather than from the if:

    case "foo":
var ret : String;
if (cond)
{
ret = "Hello";
}
else
{
ret = "World";
}
return ret;