6 Replies Latest reply on Jan 4, 2007 10:23 AM by ntsiii

    Assoc arrays

    187_2007 Level 1
      I have a CFC that is returning type of Struct. When I get to my Flex2 function how do I get an element out of the Assoc array?

      public function resultHandler1(event:ResultEvent):void
      {
      var myStruct:Array = new Array();
      trace("Struct1: " + myStruct[name]);
      }

      What's wrong with my syntex?
        • 1. Re: Assoc arrays
          ntsiii Level 3
          I don't know CF or what the type STRUCT is, but in your subject you use the word "Assoc", which I presume to be an abbreviation for "associative" array.

          If a STRUC is an associative array, then you should not type it in Flex as Array, but rather as Object.

          Also, in the code you posted, you are never setting myStruct with the result data. So try:
          var myStruct:Object = event.result;
          trace("Struct1: " + myStruct[name]); //assuming a variable named "name", containing the name of a property in the struct.

          Tracy

          • 2. Re: Assoc arrays
            187_2007 Level 1
            Correct. A Structure is an Associative Array. Okay. I took your suggestion and I got a NULL returned. Here is my CFC:

            <cffunction name="returnFlexStruct" access="public" returntype="Struct">
            <cfset employee = structnew()>
            <cfset employee.name = "Rob">
            <cfset employee.age = "27">
            <cfset employee.height = "6foot">
            <cfset employee.weight = "200">
            <cfset employee.sex = "male">
            <cfset employee.lastname = "capilli">
            <cfreturn employee>
            </cffunction>

            I tried the return type (returntype="Struct">) to Array, and it gave me an error. So I know that the Struct is comming back. When I trace out the object: trace(ObjectUtil.toString(event.result)) here is what comes back:


            Struct1: (Object)#0
            AGE = "27"
            HEIGHT = "6foot"
            LASTNAME = "capilli"
            NAME = "Rob"
            SEX = "male"
            WEIGHT = "200"

            So as you can see the Structure is returning properly to the function but for some reason I am getting a NULL when I run this function:

            public function resultHandler1(event:ResultEvent):void
            {

            var myStruct:Object = event.result;
            trace("Struct1: " + myStruct[name]);
            }

            I also tried binding the myStruct like this (further up in the Flex code):

            [Bindable]
            private var myStruct:Object = new Object();


            hummmm.... what am I missing here?
            • 3. Re: Assoc arrays
              ntsiii Level 3
              Are you sure the your "name" variable contains the string "NAME", with the correct case? You obviously are leaving some code out of what you are posting, since that variable is neither declared nor set in the resultHandler function.

              Also, trace the myStruct variable in the handler.

              Tracy
              • 4. Re: Assoc arrays
                187_2007 Level 1
                ooooooo.. okay... Here is what I did..

                public function resultHandler1(event:ResultEvent):void
                {
                var myStruct:Object = event.result;
                trace("Struct1: " + myStruct[age]);
                }


                Simply changed the "name" property to "age" property. Because it's in the associative array that is comming back from my CFC.... Here is what I noticed.... Below in my "problems" view I get this.

                1120: Access of undefined property age.


                But I thought I defined this property as object myStruct... I think I am having a brain fart here... :) Is this a better explanation?
                • 5. Re: Assoc arrays
                  187_2007 Level 1
                  AHHH HAAAAAA

                  Here is the correct notation:

                  var myStruct:Object = event.result;
                  trace("Struct1: " + myStruct.AGE);

                  Evidently when you are passing a Structure back from a CFC, in order to correctly grab the element, it has to be all uppercase. Maybe because CF is a tag based language?? I donno... I just know it works now.
                  • 6. Re: Assoc arrays
                    ntsiii Level 3
                    I thought name was a variable you'd declared and set somewhere else. This syntax is also valid:

                    var myStruct:Object = event.result;
                    var name:String = "NAME"; //or "AGE" or whatever property name you want
                    trace("Struct1: " + myStruct[name]);

                    That syntax is identical underneath to using the .property syntax. And it is more flexible, because using bracket notation, you can build the property name dynamically:
                    myStruct["NA" + "ME"]); is valid.

                    The case for the property name must match the case for the property name in the STRUCT, as shown in the property dump you did.

                    Tracy