{"id":302,"date":"2009-12-11T14:42:27","date_gmt":"2009-12-11T19:42:27","guid":{"rendered":"http:\/\/thaylin.info\/blog\/?p=302"},"modified":"2009-12-11T14:44:11","modified_gmt":"2009-12-11T19:44:11","slug":"quick-note","status":"publish","type":"post","link":"https:\/\/www.thaylin.com\/blog\/2009\/12\/11\/quick-note\/","title":{"rendered":"Dynamic class property retrieval"},"content":{"rendered":"<p>I&#8217;m taking a break from the pattern tutorials for the moment to write the as3 facebook connect integration library I&#8217;ve been meaning to write. With that said, here&#8217;s a little method I didn&#8217;t realize was around to pull properties from classes. Normally the for..in method you can pull properties available within an object, but this doesn&#8217;t work on classes you create. <!--more--><\/p>\n<p>For instance:<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nvar tempObj:Object = new Object();\r\ntempObj.prop1 = \"string1\";\r\ntempObj.prop2 = 1234;\r\ntempObj.prop3 = true;\r\nfor(var key:String in tempObj)\r\n{\r\n\ttrace(key+' : '+tempObj[key])\r\n}\r\n\/\/traces out the properties in no specific order.\r\n<\/pre>\n<p>While this works for a normal object here that is dynamic in it&#8217;s type, this same thing won&#8217;t work for a class you create. Take for instance this class that will be our new value object class.<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nclass TempObjVO\r\n{\r\npublic var prop1:String = 'string1';\r\npublic var prop2:Number = 1234;\r\npublic var prop3:Boolean = true;\r\n}\r\n<\/pre>\n<p>Now when we try to do the same thing here we get nothing;<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nvar tempObj:TempObjVO = new TempObjVO();\r\ntempObj.prop1 = \"string1\";\r\ntempObj.prop2 = 1234;\r\ntempObj.prop3 = true;\r\nfor(var key:String in tempObj)\r\n{\r\n\ttrace(key+' : '+tempObj[key])\r\n}\r\n\/\/traces nothing out\r\n<\/pre>\n<p>Luckily we&#8217;ve got something up our sleeves to find our properties. It&#8217;s called flash.utils.describeType(value:Object) and it returns an XML object.<\/p>\n<p>Now if we use this we can then retrieve xml data of this class. Like so:<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nvar tempObj:TempObjVO = new TempObjVO();\r\n\t\t\t\r\nfor(var key:String in tempObj)\r\n{\r\n\ttrace('IN THE CLIP CLASS - '+key+' : '+tempObj[key])\r\n}\r\n\t\t\t\r\ntrace(describeType(tempObj))\r\n\/\/traces out:\r\n\/\/<type name=\"TempObjVO\" base=\"Object\" isDynamic=\"false\" isFinal=\"false\" isStatic=\"false\">\r\n\/\/  <extendsClass type=\"Object\"\/>\r\n\/\/  <variable name=\"prop3\" type=\"Boolean\"\/>\r\n\/\/  <variable name=\"prop2\" type=\"Number\"\/>\r\n\/\/ <variable name=\"prop1\" type=\"String\"\/>\r\n\/\/<\/type>\r\n<\/pre>\n<p>As you can see this gives you a ton of information about our class, not just the properties, but what it extends if it&#8217;s dynamic, final, or static and the name of the class.<\/p>\n<p>So then with a little bit of code we can pull the class properties and set them to what we want.<br \/>\nFor this part we&#8217;ll create two of the same value objects, one with the default values and one with our new values. Normally, maybe you would want to pass in a class object to some method that then updates a different class object with values from that object that are also on the new object. Or something to that affect. Anyway, here is our code:<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nvar obj:TempObjVO = new TempObjVO();\r\nobj.prop1 = \"different string\";\r\nobj.prop2 = 6543;\r\nobj.prop3 = false;\r\n\t\t\t\r\nvar tempObj:TempObjVO = new TempObjVO();\r\n\r\n\t\t\t\r\nfor each (var variable:XML in describeType(obj).variable)\r\n{\r\n\tif(tempObj.hasOwnProperty(variable.@name))\r\n\t{\r\n\t\ttrace('CHANGING '+variable.@name+'in tempObj from '+tempObj[variable.@name]+'  to  '+obj[variable.@name])\r\n\t\ttempObj[variable.@name] = obj[variable.@name];\r\n\t} \r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m taking a break from the pattern tutorials for the moment to write the as3 facebook connect integration library I&#8217;ve been meaning to write. With that said, here&#8217;s a little method I didn&#8217;t realize was around to pull properties from classes. Normally the for..in method you can pull properties available within an object, but this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,6,8],"tags":[32,16,31],"_links":{"self":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/302"}],"collection":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":8,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/302\/revisions\/309"}],"wp:attachment":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}