<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Creating a component instance by class name in ActionScript 3.0</title>
	<atom:link href="http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Sun, 12 Feb 2012 19:26:49 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Łukasz Wilk</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-10102</link>
		<dc:creator>Łukasz Wilk</dc:creator>
		<pubDate>Fri, 28 Oct 2011 11:58:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-10102</guid>
		<description>Hi, I know it&#039;s old thread but I got similar problem as andrew and can&#039;t find straight answer. All would be great but I use addElement in place of addChild. For examples sake let&#039;s say I add a button with id &quot;button1&quot;. If I use mxml I can simply call it by using this[&quot;button1&quot;], but when I use pure ActrinScript I get an error saying that value doesn&#039;t exists or is null. Only way I found so was was to make custom array and add reference to this component (array_name[&quot;button1&quot;]), but I don&#039;t really like this way around and want to use this[&quot;button1&quot;] same like for mxml components. Could you point me in the right direction?</description>
		<content:encoded><![CDATA[<p>Hi, I know it&#8217;s old thread but I got similar problem as andrew and can&#8217;t find straight answer. All would be great but I use addElement in place of addChild. For examples sake let&#8217;s say I add a button with id &#8220;button1&#8243;. If I use mxml I can simply call it by using this["button1"], but when I use pure ActrinScript I get an error saying that value doesn&#8217;t exists or is null. Only way I found so was was to make custom array and add reference to this component (array_name["button1"]), but I don&#8217;t really like this way around and want to use this["button1"] same like for mxml components. Could you point me in the right direction?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rany</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3761</link>
		<dc:creator>Rany</dc:creator>
		<pubDate>Wed, 17 Jun 2009 18:04:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3761</guid>
		<description>solved the problem for icon/image class

[Bindable][Embed(”assets/iconset/save.png”)]
public var saveIcon:Class;

--------------------------------------------------
var iconSource:String = &quot;saveIcon&quot;;

var ImageClass:Class = this[iconSource] as Class;
button.setStyle(&quot;icon&quot;, ImageClass);</description>
		<content:encoded><![CDATA[<p>solved the problem for icon/image class</p>
<p>[Bindable][Embed(”assets/iconset/save.png”)]<br />
public var saveIcon:Class;</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
var iconSource:String = &#8220;saveIcon&#8221;;</p>
<p>var ImageClass:Class = this[iconSource] as Class;<br />
button.setStyle(&#8220;icon&#8221;, ImageClass);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rany</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3753</link>
		<dc:creator>Rany</dc:creator>
		<pubDate>Wed, 17 Jun 2009 17:31:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3753</guid>
		<description>How about an icon or image class

e.g.

[Bindable][Embed(&quot;assets/iconset/save.png&quot;)]
public var saveIcon:Class;</description>
		<content:encoded><![CDATA[<p>How about an icon or image class</p>
<p>e.g.</p>
<p>[Bindable][Embed("assets/iconset/save.png")]<br />
public var saveIcon:Class;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3754</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Fri, 02 Jan 2009 17:46:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3754</guid>
		<description>Quote &quot;we must include a reference to each of the components we may need to create at runtime in a variable&quot;.

How do we include a dynamic reference?  Say, I have my own component called &quot;MyButton&quot;, which is dynamically loaded from database or xml file at runtime.  In another word, I don&#039;t the class name of my component at the coding/compiling time</description>
		<content:encoded><![CDATA[<p>Quote &#8220;we must include a reference to each of the components we may need to create at runtime in a variable&#8221;.</p>
<p>How do we include a dynamic reference?  Say, I have my own component called &#8220;MyButton&#8221;, which is dynamically loaded from database or xml file at runtime.  In another word, I don&#8217;t the class name of my component at the coding/compiling time</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: andrew</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3759</link>
		<dc:creator>andrew</dc:creator>
		<pubDate>Wed, 03 Sep 2008 20:05:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3759</guid>
		<description>Yes! That was it.

It took me about 5 hours to figure this one out.

You rock!

Andrew.</description>
		<content:encoded><![CDATA[<p>Yes! That was it.</p>
<p>It took me about 5 hours to figure this one out.</p>
<p>You rock!</p>
<p>Andrew.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterd</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3758</link>
		<dc:creator>peterd</dc:creator>
		<pubDate>Wed, 03 Sep 2008 19:44:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3758</guid>
		<description>andrew,

Try using &lt;code&gt;&lt;strong&gt;main.&lt;/strong&gt;getChildByName()&lt;/code&gt; instead:
&lt;pre class=&quot;code&quot;&gt;
var i:Image = &lt;strong style=&quot;color:red;&quot;&gt;main.&lt;/strong&gt;getChildByName(&quot;54237&quot;) as Image;
&lt;/pre&gt;

Peter</description>
		<content:encoded><![CDATA[<p>andrew,</p>
<p>Try using <code><strong>main.</strong>getChildByName()</code> instead:</p>
<pre class="code">
var i:Image = <strong style="color:red;">main.</strong>getChildByName(&quot;54237&quot;) as Image;
</pre>
<p>Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: andrew</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3757</link>
		<dc:creator>andrew</dc:creator>
		<pubDate>Wed, 03 Sep 2008 18:47:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3757</guid>
		<description>...note that the second line should read:
var obj:UIComponent = new c();</description>
		<content:encoded><![CDATA[<p>&#8230;note that the second line should read:<br />
var obj:UIComponent = new c();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: andrew</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3756</link>
		<dc:creator>andrew</dc:creator>
		<pubDate>Wed, 03 Sep 2008 18:42:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3756</guid>
		<description>Thank you so much Peter - that makes a lot of sense.However, for some reason, my code is throwing an error: &quot;Cannot accesss the property of a null object...&quot;


This is my code in a nutshell:

The following code is in a loop where &quot;contentType&quot; changes between Text, TextArea and Image. It outputs perfectly (thanks to you!).

&lt;pre class=&quot;code&quot;&gt;
var c:Class = getDefinitionByName(&#039;mx.controls.&#039;+contentType) as Class;
var obj:UIComponent = new c();

if (obj) {
switch (obj.className) {
    case &quot;Text&quot;:
        Text(obj).id = contentID;
        Text(obj).text = content;
        Text(obj).x = contentxPos;
        Text(obj).y = contentyPos;
        Text(obj).width = contentWidth;
        Text(obj).height = contentHeight;
        Text(obj).styleName = contentStyleName;
        break;
    case &quot;TextArea&quot;:
        TextArea(obj).id = contentID;
        TextArea(obj).text = content;
        TextArea(obj).x = contentxPos;
        TextArea(obj).y = contentyPos;
        TextArea(obj).width = contentWidth;
        TextArea(obj).height = contentHeight;
        TextArea(obj).styleName = contentStyleName;
        break;
    case &quot;Image&quot;:
        Image(obj).name = &quot;54237&quot;;
        Image(obj).id = contentID;
        Image(obj).source = contentSource;
        Image(obj).x = contentxPos;
        Image(obj).y = contentyPos;
        Image(obj).width = contentWidth;
        Image(obj).height = contentHeight;
        Image(obj).setStyle(&#039;completeEffect&#039;, slowFade);
        Image(obj).addEventListener(MouseEvent.CLICK, showImageGallery);
        Image(obj).addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
        break;
}
main.addChild(obj);
}
&lt;/pre&gt;


...but then I use:
&lt;pre class=&quot;code&quot;&gt;
var i:Image = getChildByName(&quot;54237&quot;) as Image;
i.source = &#039;/myimage.jpg&#039;;
&lt;/pre&gt;

And it doesn&#039;t work.  Note that I just hardcoded &quot;54237&quot; for testing...

Any suggestions ???

Thanks again!</description>
		<content:encoded><![CDATA[<p>Thank you so much Peter &#8211; that makes a lot of sense.However, for some reason, my code is throwing an error: &#8220;Cannot accesss the property of a null object&#8230;&#8221;</p>
<p>This is my code in a nutshell:</p>
<p>The following code is in a loop where &#8220;contentType&#8221; changes between Text, TextArea and Image. It outputs perfectly (thanks to you!).</p>
<pre class="code">
var c:Class = getDefinitionByName('mx.controls.'+contentType) as Class;
var obj:UIComponent = new c();

if (obj) {
switch (obj.className) {
    case "Text":
        Text(obj).id = contentID;
        Text(obj).text = content;
        Text(obj).x = contentxPos;
        Text(obj).y = contentyPos;
        Text(obj).width = contentWidth;
        Text(obj).height = contentHeight;
        Text(obj).styleName = contentStyleName;
        break;
    case "TextArea":
        TextArea(obj).id = contentID;
        TextArea(obj).text = content;
        TextArea(obj).x = contentxPos;
        TextArea(obj).y = contentyPos;
        TextArea(obj).width = contentWidth;
        TextArea(obj).height = contentHeight;
        TextArea(obj).styleName = contentStyleName;
        break;
    case "Image":
        Image(obj).name = "54237";
        Image(obj).id = contentID;
        Image(obj).source = contentSource;
        Image(obj).x = contentxPos;
        Image(obj).y = contentyPos;
        Image(obj).width = contentWidth;
        Image(obj).height = contentHeight;
        Image(obj).setStyle('completeEffect', slowFade);
        Image(obj).addEventListener(MouseEvent.CLICK, showImageGallery);
        Image(obj).addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
        break;
}
main.addChild(obj);
}
</pre>
<p>&#8230;but then I use:</p>
<pre class="code">
var i:Image = getChildByName("54237") as Image;
i.source = '/myimage.jpg';
</pre>
<p>And it doesn&#8217;t work.  Note that I just hardcoded &#8220;54237&#8243; for testing&#8230;</p>
<p>Any suggestions ???</p>
<p>Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterd</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3760</link>
		<dc:creator>peterd</dc:creator>
		<pubDate>Wed, 03 Sep 2008 17:32:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3760</guid>
		<description>andrew,

You could use &lt;code&gt;getChildAt()&lt;/code&gt; or &lt;code&gt;getChildByName()&lt;/code&gt; to access the dynamically created component instances. Something like the following:

&lt;pre class=&quot;code&quot;&gt;
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
        layout=&quot;vertical&quot;
        verticalAlign=&quot;middle&quot;
        backgroundColor=&quot;white&quot;
        creationComplete=&quot;init();&quot;&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import flash.utils.getDefinitionByName;
            import mx.controls.Button;

            private const dummyArr:Array = [Button];

            private function init():void {
                var c:Class = getDefinitionByName(&quot;mx.controls.Button&quot;) as Class;
                var idx:uint;
                var len:uint = 5;
                for (idx = 0; idx &lt; len; idx++) {
                    var obj:Button = new c();
                    obj.label = &quot;Button &quot; + idx;
                    obj.name = &quot;button&quot; + idx;
                    addChild(obj);
                }
            }

            private function btn_click(evt:MouseEvent):void {
                var b:Button = getChildByName(&quot;button3&quot;) as Button;
                b.label = &quot;OUCH&quot;;
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock=&quot;true&quot;&gt;
        &lt;mx:Button id=&quot;btn&quot;
                label=&quot;Change Button 3&quot;
                click=&quot;btn_click(event);&quot; /&gt;
    &lt;/mx:ApplicationControlBar&gt;

&lt;/mx:Application&gt;
&lt;/pre&gt;

Peter</description>
		<content:encoded><![CDATA[<p>andrew,</p>
<p>You could use <code>getChildAt()</code> or <code>getChildByName()</code> to access the dynamically created component instances. Something like the following:</p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import flash.utils.getDefinitionByName;
            import mx.controls.Button;

            private const dummyArr:Array = [Button];

            private function init():void {
                var c:Class = getDefinitionByName("mx.controls.Button") as Class;
                var idx:uint;
                var len:uint = 5;
                for (idx = 0; idx &lt; len; idx++) {
                    var obj:Button = new c();
                    obj.label = "Button " + idx;
                    obj.name = "button" + idx;
                    addChild(obj);
                }
            }

            private function btn_click(evt:MouseEvent):void {
                var b:Button = getChildByName("button3") as Button;
                b.label = "OUCH";
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button id="btn"
                label="Change Button 3"
                click="btn_click(event);" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

&lt;/mx:Application&gt;
</pre>
<p>Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: andrew</title>
		<link>http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/comment-page-1/#comment-3755</link>
		<dc:creator>andrew</dc:creator>
		<pubDate>Wed, 03 Sep 2008 16:54:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/28/creating-a-component-instance-by-class-name-in-actionscript-30/#comment-3755</guid>
		<description>Thanks for the code. Works perfect.

Let&#039;s say you created 10 components. And then you want to reference those components in actionscript. How would you do so?

For example, let&#039;s say you wanted to dynamically change the background of one of the components. How you you write the actionscript (instance.backgrouColor = #000000 ??? )

Andrew.</description>
		<content:encoded><![CDATA[<p>Thanks for the code. Works perfect.</p>
<p>Let&#8217;s say you created 10 components. And then you want to reference those components in actionscript. How would you do so?</p>
<p>For example, let&#8217;s say you wanted to dynamically change the background of one of the components. How you you write the actionscript (instance.backgrouColor = #000000 ??? )</p>
<p>Andrew.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

