4 Replies Latest reply on Aug 2, 2007 3:46 PM by ntsiii

    length() not length!

    Jorshasaur
      I ran into this problem a few days ago, and I thought I'd share it since I couldn't find any mention of it in the AS2 -> AS3 conversion documentation from Adobe. In my opinion its a huge change and one that could possibly keep folks up late at night trying to figure out.

      The problem is that length used to be a property of relevant objects, and now it is treated like a function. What's worse, is that if you use length the way it was handled in AS2, you won't get an error from either the compiler or from the IDE. Here's an example:

      for(var k:Number = 0; k < subnav. length; k++){

      Noticed how I left the parantheses off of length? That's what we've all been used to before, this worked fine under AS2. Length was treated as a property. However under AS3 this will not work. You won't get an error at all, it will just fail. If you trace subnav.length, you won't get a value - you won't even get null. It traces back as an empty space.

      for(var k:Number = 0; k < subnav. length(); k++){

      Now with the parantheses the code works just like you'd expect it to!

      I'm not sure why Adobe has made the length property a function now instead of a property, but I really feel like it deserves documentation for those of us who come from AS2. Add to the fact that this problem does not return any sort of error, and you've got one potentially nasty time sink. Hope this helps someone out there!
        • 1. Re: length() not length!
          JoeADSK
          hmmm i have been using .length without () and it has been working just fine

          you sure about that?
          • 2. Re: length() not length!
            Kenny Yates Level 2
            Actually both of you are right....

            I have found the following situations:
            If your data is of type ArrayCollection or Array, etc. you can use the "length" property.
            If your data is of type XML or XMLList, etc you use "length()" function.

            I am sure there are other types that fall under both categories but that is just the few that I can think of off the top of my head that I have run across.

            And yes, Jorshasaur is correct it will compile no errors thrown and just does bascially nothing so if you expect data and know all else is good, debug that issue.

            Hope this clarifies this a bit.

            Kenny
            • 3. Re: length() not length!
              Mitek17 Level 1
              Just a note - why people are using Number data type in simple loops or counters?
              Number - is a float double precision value. And you will face all consequences of real numbers, like 20/2 = 10.0000000002

              Use int or uint - they are far more efficient for this.

              Get real, use int :)
              • 4. Re: length() not length!
                ntsiii Level 3
                No excuse, just a matter of time to go back and change that code. int and uint are new and Number still runs...

                It IS best prectice to use the most restrictive data type that fits your requirements.

                Tracy