Re: [Flashcoders] Call method by name: A few questions...
Here is my latest solution: Document class: === import com.foo.util.GoogleMaps; ... _map.controls = ['PositionControl', 'ZoomControl', 'MapTypeControl', 'ScaleControl', 'OverviewMapControl']; === Here's com.foo.util.GoogleMaps: === import com.google.maps.controls.*; ... private function onMapReady($e:Event):void { for each (var s:String in _controls) { this['my' + s](); } }; ... private function myZoomControl():void { _map.addControl(new ZoomControl()); }; private function myPositionControl():void { _map.addControl(new PositionControl()); }; private function myMapTypeControl():void { _map.addControl(new MapTypeControl()); }; private function myScaleControl():void { _map.addControl(new MapTypeControl()); }; private function myOverviewMapControl():void { _map.addControl(new MapTypeControl()); } ... public function set controls($a:Array):void { _controls = $a; }; === I am sure to all ya'll pro AS3 coders, the above looks pretty silly... :) I don't think I will actually use the above code in my final program... I just thought I would share a working solution. Thanks again for all of the help! I really appreciate it. :) Cheers, Micky ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Call method by name: A few questions...
Hi All! Many thanks for the replies, I really appreciate the pro help. :) See inline replies below: On Fri, Jan 14, 2011 at 8:48 PM, Dave Watts wrote: > I'm not sure if this is what you're asking for. Doh, sorry if my question was a little convoluted and ambiguous. :( > new com.google.maps.controls.ZoomControl(); Thanks for tip! In this case, I would like to do something like this: for each (var s:String in _controls) { new com.google.maps.controls[s](); } Unfortunately, that does not work. :( > Not if you have a property of the object that is an array. Ahh, not in this case. My document class is calling the "controls" setter like so: _map.controls = ['PositionControl', 'ZoomControl', 'MapTypeControl', 'ScaleControl', 'OverviewMapControl']; Where each is a class method of com.google.maps.controls. Based on your feedback, it is sounding like what I am trying to do is bad practice. Basically, I was just trying to avoid writing a setter for each type of map control. On Sat, Jan 15, 2011 at 1:31 AM, Henrik Andersson wrote: > You want the getDefinitionByName function, not the dynamic property access > syntax. Ah, interesting! You know, I come across this function via Google, but I was not able to grasp how to properly use it in this situation... This is the class: com.google.maps.controls And this is a method of the class (for example): ZoomControl() I have tried a few different variations of the below code with no success: var classRef:Class = getDefinitionByName('com.google.maps'); for each (var s:String in _controls) { // Test #1: //var classRef:Class = getDefinitionByName('com.google.maps.controls.' + s + '()'); //var controls = new classRef(); // Test #2: new classRef.controls[s](); } Nine times out of ten I get this error: [[ Implicit coercion of a value with static type Object to a possibly unrelated type Class. ]] On Sat, Jan 15, 2011 at 2:14 AM, Leandro Ferreira wrote: > You must declare each class or they are not gonna be compiled. Just put all > the class names you need separated by comma: ZoomControl, PositionControl, > ScaleControl; This sounds interesting! Could you clarify a bit further? I am not sure where I should do this. Thanks again everyone! I greatly appreciate the pro assists on this one. :) Cheers, Micky ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Call method by name: A few questions...
You must declare each class or they are not gonna be compiled. Just put all the class names you need separated by comma: ZoomControl, PositionControl, ScaleControl; * * * @leandroferreira* * 55 61 91151257* On Sat, Jan 15, 2011 at 07:31, Henrik Andersson wrote: > You want the getDefinitionByName function, not the dynamic property access > syntax. > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Call method by name: A few questions...
You want the getDefinitionByName function, not the dynamic property access syntax. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Call method by name: A few questions...
I'm not sure if this is what you're asking for. > 1. How to call com.google.maps.controls.* methods by name? new com.google.maps.controls.ZoomControl(); > 2. Is it bad practice to pass an array to a setter? Not if you have a property of the object that is an array. Dave Watts, CTO, Fig Leaf Software http://www.figleaf.com/ http://training.figleaf.com/ Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on GSA Schedule, and provides the highest caliber vendor-authorized instruction at our training centers, online, or onsite. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Call method by name: A few questions...
Abbreviated AS3: == import com.google.maps.controls.*; // Setter: public function set controls($a:Array):void { _controls = $a; // Array of string method names. }; private function onMapReady($e:Event):void { // This works: _map.addControl(new ZoomControl()); // This does not work: for each (var s:String in _controls) { // Call methods by name... But, how? _map.addControl(new this[s]()); // Gives me error. // How to refer to the imported "com.google.maps.controls" instead of "this"? } }; == I really want to call com.google.maps.controls.* methods by name, but when I try (see "new this[s]()" line above) I get this error: [[ Property ZoomControl not found on my.package.util.GoogleMaps and there is no default value. ]] Where "ZoomControl" is just one of the possible control methods that I want to call. Questions: 1. How to call com.google.maps.controls.* methods by name? 2. Is it bad practice to pass an array to a setter? 3. If #2 answer is yes, would it be best to just have one setter per "control" method? Am I making any sense here? :D Please let me know if I can clarify further. Thanks! Micky ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders