# Accesing mxml components from AS class (flex 3)

**URL:** <https://forum.kirupa.com/t/accesing-mxml-components-from-as-class-flex-3/260923>\
**Category:** flash\
**Created:** [May 21, 2008, 2:46am UTC](https://forum.kirupa.com/t/accesing-mxml-components-from-as-class-flex-3/260923 "2008-05-21T02:46:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goto](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@goto](https://forum.kirupa.com/u/goto)\
**Post date:** [May 21, 2008, 2:46am UTC](https://forum.kirupa.com/t/accesing-mxml-components-from-as-class-flex-3/260923/1 "2008-05-21T02:46:36Z")

</div>

[FONT=Tahoma][SIZE=3]hey everybody,  
I have a big deal here, and havent be able to solve it.  
Im creating a flex chat, with php sockets and the AS3 xmlSocket class basically.  
I have a main class Chat, there I stablish the connection, send and receive the messages. However, I have seen how it is possible to get access to a button or textArea from an AS script wrote in the main mxml file, I mean, something like this works clearly:

[/SIZE][/FONT] ActionScript Code:  
[LEFT]\<mx:Script\>\<![COLOR=#000000][[/COLOR]CDATA[COLOR=#000000][[/COLOR]  
[COLOR=#0000ff]private[/COLOR] [COLOR=#000000] **function** [/COLOR] setLabelCOLOR=#000000[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000ff]if[/COLOR] [COLOR=#000000]([/COLOR]myButton.[COLOR=#000080]label[/COLOR]==[COLOR=#ff0000]“Click Me”[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
myButton.[COLOR=#000080]label[/COLOR] = [COLOR=#ff0000]“Clicked”[/COLOR];  
[COLOR=#000000]}[/COLOR] [COLOR=#0000ff]else[/COLOR] [COLOR=#000000]{[/COLOR]  
myButton.[COLOR=#000080]label[/COLOR] = [COLOR=#ff0000]“Click Me”[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]][/COLOR][COLOR=#000000]][/COLOR]\>\</mx:Script\>

```
&lt;mx:[COLOR=#0000ff]Button[/COLOR] id=[COLOR=#ff0000]"myButton"[/COLOR] label=[COLOR=#ff0000]"Click Me"[/COLOR] click=[COLOR=#ff0000]"setLabel();"[/COLOR]/&gt;

```

[/LEFT]

[FONT=Tahoma][SIZE=3]  
[/SIZE][/FONT][LEFT][LEFT][LEFT]However, when I try to access the same way, but now from my class it does not recognize Im referencing a component at mx.core.Application. I have tried to pass the reference from the application to the class (just as you we do in Flash) but it doesnt work. So my problem is \*\*How do I access mxml components in the main application from an ActionScript class \*\*, I get the messages from the socket and I need to write them on a textArea.  
This is how I import the class  
ActionScript Code:  
[LEFT]\<mx:Script\>  
\<![COLOR=#000000][[/COLOR]CDATA[COLOR=#000000][[/COLOR]  
[COLOR=#0000ff]import[/COLOR] com.[COLOR=#000080]Chat[/COLOR];  
[COLOR=#0000ff]public[/COLOR] [COLOR=#000000] **function** [/COLOR] initAppCOLOR=#000000[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#000000] **var** [/COLOR] myChat = [COLOR=#000000] **new** [/COLOR] ChatCOLOR=#000000[/COLOR];  
myChat.[COLOR=#0000ff]Connect[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]][/COLOR][COLOR=#000000]][/COLOR]\>  
\</mx:Script\>  
[/LEFT]

And my classs:…

```
 ActionScript Code:
 [LEFT]package com [COLOR=#000000]{[/COLOR]

```

[COLOR=#0000ff]import[/COLOR] flash.[COLOR=#000080]events[/COLOR].\*;  
[COLOR=#0000ff]import[/COLOR] flash.[COLOR=#000080]net[/COLOR].[COLOR=#0000ff]XMLSocket[/COLOR];

```
[COLOR=#0000ff]public[/COLOR] [COLOR=#000000] **class** [/COLOR] Chat [COLOR=#000000]{[/COLOR]
    
    
        [COLOR=#808080]*//private var hostName:String = "192.168.0.102";*[/COLOR]
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000] **var** [/COLOR] hostName:[COLOR=#0000ff]String[/COLOR] = [COLOR=#ff0000]"192.168.0.123"[/COLOR];
        [COLOR=#808080]*//private var hostName:String = "192.168.0.106";*[/COLOR]
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000] **var** [/COLOR] port:uint = [COLOR=#000080]9999[/COLOR];
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000] **var** [/COLOR] socket:[COLOR=#0000ff]XMLSocket[/COLOR];
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000] **var** [/COLOR] usern:[COLOR=#0000ff]String[/COLOR]=[COLOR=#ff0000]""[/COLOR];
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000] **var** [/COLOR] head:[COLOR=#0000ff]String[/COLOR],body:[COLOR=#0000ff]String[/COLOR], msg:[COLOR=#0000ff]String[/COLOR];
        [COLOR=#0000ff]private[/COLOR] [COLOR=#000000] **var** [/COLOR] window;

        
        [COLOR=#0000ff]public[/COLOR] [COLOR=#000000] **function** [/COLOR] Chat[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
            [COLOR=#808080]*//usern = "Xtian";*[/COLOR]
            socket = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000ff]XMLSocket[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            configureListeners[COLOR=#000000]([/COLOR]socket[COLOR=#000000])[/COLOR];
        [COLOR=#000000]}[/COLOR]
        
        [COLOR=#0000ff]public[/COLOR] [COLOR=#000000] **function** [/COLOR] setUsername[COLOR=#000000]([/COLOR]user[COLOR=#000000])[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]
            [COLOR=#0000ff]this[/COLOR].[COLOR=#000080]usern[/COLOR] = user;
        [COLOR=#000000]}[/COLOR]

        [COLOR=#0000ff]public[/COLOR] [COLOR=#000000] **function** [/COLOR] [COLOR=#0000ff]Connect[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]:[COLOR=#0000ff]void[/COLOR] [COLOR=#000000]{[/COLOR]
            socket.[COLOR=#0000ff]connect[/COLOR][COLOR=#000000]([/COLOR]hostName, port[COLOR=#000000])[/COLOR];
            msgArea.[COLOR=#0000ff]text[/COLOR] += [COLOR=#ff0000]"Connecting to server[COLOR=#000099]**

```

\*\*[/COLOR]"[/COLOR];

…  
[/LEFT]

The problem is right here!  
ActionScript Code:  
[LEFT]msgArea.[COLOR=#0000ff]text[/COLOR] += [COLOR=#ff0000]“Connecting to server[COLOR=#000099]\*\*  
\*\*[/COLOR]”[/COLOR];  
[/LEFT]

msgArea is a TextArea on the main Application.

Thanks for any help, I hope everything is clear  
::Goto::

[/LEFT]  
[/LEFT]  
[/LEFT]

---

<div class="post-metadata">

**Author:** ![goto](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@goto](https://forum.kirupa.com/u/goto)\
**Post date:** [May 21, 2008, 9:25pm UTC](https://forum.kirupa.com/t/accesing-mxml-components-from-as-class-flex-3/260923/2 "2008-05-21T21:25:13Z")

</div>

[quote=amarghosh;2329024] ActionScript Code:  
[LEFT][COLOR=#0000FF]public[/COLOR] [COLOR=#000000] **class** [/COLOR] Chat  
[COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **var** [/COLOR] app:MainMXMLFileName;  
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000] **function** [/COLOR] ChatCOLOR=#000000[/COLOR]  
[COLOR=#000000]{[/COLOR]  
app = application;  
[COLOR=#808080]\*//now u can call \*[/COLOR]  
app.[COLOR=#000080]textAreaID[/COLOR].[COLOR=#0000FF]text[/COLOR] = [COLOR=#FF0000]“blah blah foo”[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR]

[COLOR=#808080]_//from mxml script of main application class;_[/COLOR]  
[COLOR=#000000] **var** [/COLOR] chat:Chat = [COLOR=#000000] **new** [/COLOR] ChatCOLOR=#000000[/COLOR];  
[/LEFT]

tell me if it works – am just guessing :)[/quote]

hey,  
Well I tried it before, because its what we generally do on flash, however it seems to be a conflict between data types or it just dont get the reference to the main application that easy. But thank you anyway 🙂

---

<div class="post-metadata">

**Author:** ![goto](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@goto](https://forum.kirupa.com/u/goto)\
**Post date:** [May 22, 2008, 5:25am UTC](https://forum.kirupa.com/t/accesing-mxml-components-from-as-class-flex-3/260923/3 "2008-05-22T05:25:16Z")

</div>

[quote=amarghosh;2329460]change constructor argument type to Object and give a trace in Chat constructor to see what it really receives;

another work around i think will work is:  
Try passing _this.parentApplication_ instead of _this_. Chat constructor should listen for Object rather than any particular type; then give a trace in constructor to find what was really the data type of it;

```
ActionScript Code:
[LEFT][COLOR=#0000FF]public[/COLOR] [COLOR=#000000] **function** [/COLOR] Chat[COLOR=#000000]([/COLOR]app:[COLOR=#0000FF]Object[/COLOR][COLOR=#000000])[/COLOR]

```

[COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR];[COLOR=#808080]_//[Object ?]_[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[COLOR=#808080]_//call it from mxml_[/COLOR]  
[COLOR=#000000] **var** [/COLOR] chat:Chat = [COLOR=#000000] **new** [/COLOR] ChatCOLOR=#000000[/COLOR];  
[/LEFT]

[/quote]

Got it man! Thank you…  
pass ‘this’ works, I think the datatype was the problem, or I was just making a big mistake  
Anyway thank you, Im still a little silly in flex

::Goto::
