if-statements – checking strings for being null

hello again!
Yesterday I wrote some code again and had to check if a String is null or not. The check was performed in a for-loop.
Below is some example code:

public var arrToFill:Array = [];
  1. [some more code here …]
  2. private function foo():void
  3. {
  4.   var i:uint;
  5.   for( i = 0; i < 10; ++i)
  6.   {
  7.     if(!arrToFill[i])
  8.     {
  9.       arrToFill[i] = '';
  10.     }
  11.   }
  12. }

This function was called different times and I wondered, that the if-statement returned false everytime.
For me it was theoretically correct that an Array-Element with a value of ” is not empty, because ” is an empty String for me, but it is something.
ActionScript3 doesn’t work like that, so for AS3 a String with a value of ” is equal to null or 0.

Conclusion:
If you have code like this and want to preallocate an element, just set it to 1. Then you can do a simpy

if(!arrToFill[i])

and everything would be fine.

Leave a comment

Name: (required)

E-Mail: (required)

Website:

Comment: