3 Replies Latest reply on Jul 30, 2006 6:47 PM by isml

    Question about Object class

    isml Level 1
      In Flex 1.5, there is no Map class. Some friends tell me a useful trick: Use Object to simulate a map.
      To say it clearly, if i need a Number to String Map(For example , a student id to student name map), i would do like this:
      var mapIdToName:Object = new Object();
      var nId = .....;
      var strName= ...;
      mapIdToName["Stu_" + nId] = strName;

      It really works well in 1.5. And i am glad to see that the Object Class in Flex 2.0 is still a Dynamic Class, which means I could still use that old trick. I test it, and it did work. But a big problem is that an exception error is threw when i add a property to the Object map.I know this is because i reference an undefined property of Object.

      So two questions:
      1. Is there a real Map Class in Flex 2.0? If you know , please tell me where to find it, thanks
      2 How to use the old trick without getting an exception error.

      thanks in advance.
        • 1. Re: Question about Object class
          isml Level 1
          Oh, i'm very very sorry for my silly mistake.
          After checking my codes again, i found the real reason why i received exception error.I forget to new it before i add property to the map object. So the exception error shows me that i reference a Null Object , not that i reference an undefined property of this object.
          Anyway, now i know if i add a property to a dynamic-class object, no error will be threw :)

          Sorry for wasting your time again.And i still hope that you could give me an advice to the first question. As i konw, Most Map in other language uses Hash algorithm(or something like that) to improve performance. So i wonder if i could a real map to replace the old trick.


          • 2. Re: Question about Object class
            inlineblue Level 1
            Using an Object as a map is not actually a "trick". The Adobe engineers meant for it to be used this way, that's why there is no map object. I haven't seen any documentation as to how Object implements its lookup (be it a hash or otherwise), but given how heavily this functionality is used, I doubt they would've implemented a naive algorithm. And in the end, if it isn't causing any performance bottlenecks, then it's all good.
            • 3. Re: Question about Object class
              isml Level 1
              Thanks for your advice.
              Now that it is recommended, I'd use it more bravely.