8 Replies Latest reply on Jul 11, 2006 8:12 PM by doug777

    How to access data in an object

    doug777 Level 1
      Data returned from a database ends up in an object in AS where each index from 0 to number of rows returned - 1, contains another object containing the list of column names and valules. If I want to read the value of the clumn named say Col4 in row 2 (which will be in index 1, I would expect to write:

      myValue = myObject[1]["Col4"];

      This returns the error: Cannot access a property or method of a null object reference.

      By doing an - Alert.show(ObjectUtil.toString(myObject)); - I can see that the data I want is present at this index number.

      What do I write to access this data??

      Doug
        • 1. Re: How to access data in an object
          doug777 Level 1
          Sorry, I now realize that the problem is that I need to know how many rows have been returned and I've called .length on the Object which is not possible.

          So how do I find out how many rows there are, or how many index numbers are in use in the Object?

          Doug
          • 2. Re: How to access data in an object
            doug777 Level 1
            Sorry, finally turns out that it was a timing error.

            And you can call length on an Object even though it isn't included in the pop-up list of properties available in Flex Builder, or mentioned in the Language Reference.

            Is this future-proof or is there a proper way to do this??

            Doug
            • 3. Re: How to access data in an object
              inlineblue Level 1
              Data returned from a webservice call (or similar) are simply typed as Object because there' s no way of telling at compile time exactly what kind of object is going to be returned. It's up to you to know what gets returned and use it appropriately. In this case, most likely an Array was returned from your service call, which is why you can go ahead and use the length property. To make it more explicit in your code, you can cast it first:

              var myArray: Array = myObject as Array;

              This way, it's more obvious in the code and the cast will throw an exception if you didn't receive an array (a good error check).
              • 4. Re: How to access data in an object
                doug777 Level 1
                Thanks for your reply. That explains a lot.

                What is returned from a call to a database through CF is I think called an ArrayCollection in AS, but I can't quite work out what this is as the information given for this type is meagre to say the least. After all, multi-dimensional arrays are supported, and I can't quite see what an ArrayCollection is if it isn't a multi-dimensional array.

                Anyway for now I've cast the arriving data type to ArrayCollection and this certainly works correctly.

                I keep getting caught out by timing problems though. Is there a way to broadcast an event when a certain line is reached in the code somewhere?

                Doug
                • 5. Re: How to access data in an object
                  inlineblue Level 1
                  quote:

                  Originally posted by: doug777
                  I keep getting caught out by timing problems though. Is there a way to broadcast an event when a certain line is reached in the code somewhere?


                  You could use the debugger and set a breakpoint...or am I not understanding you correctly?
                  • 6. Re: How to access data in an object
                    doug777 Level 1
                    When data is received from a remote object call, a listener detects the returned data and I put this into my ArrayCollection variable.

                    If, somewhere else, I use this data, I would like to know when it is actually available for use. At the moment I am having to daisy-chain the major steps in the program by calling the next stage from the line after setting the ArrayCollection within the database-call-listener.

                    But there are lots of database calls and after a while it's hard to follow where you are in the daisy-chain and what's available and what isn't.

                    And secondly when casting, is it better to use the new as construction:

                    var myDataSet:ArrayCollection = myObject as ArrayCollection;
                    or the traditional cast
                    var myDataSet:ArrayCollection = ArrayCollection(myObject);

                    Doug
                    • 7. Re: How to access data in an object
                      inlineblue Level 1
                      Ok, then judging by your earlier question, I assume you would like to dispatch an event when you've assigned your ArrayCollection variable. You can dispatch an event using any class that implements IEventDispatcher (in practice, that means using EventDispatcher or one of its many subclasses). I don't know how your code is structured, but maybe you can create a global event dispatcher to inform of data availability.

                      As for casting, I prefer the traditional cast. In my example, I had to use the "as" operator because casting with Array() actually returns a copy of the data (I don't know why, but that's what the compiler warning told me). Also, the traditional cast will throw a type mismatch exception if the types are not compatible, while the "as" operator will simply return null, which will most likely cause a null reference exception at some later point, but that error is not as useful.
                      • 8. Re: How to access data in an object
                        doug777 Level 1
                        Thanks very much for your reply.

                        I will do some testing with EventDispatcher and see if I can create a better world for my ArrayCollections to live in.

                        Doug