# Variables in a class

**URL:** <https://forum.kirupa.com/t/variables-in-a-class/262628>\
**Category:** flash\
**Created:** [June 8, 2008, 5:12am UTC](https://forum.kirupa.com/t/variables-in-a-class/262628 "2008-06-08T05:12:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikomax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nikomax/32/3664_2.png) [@nikomax](https://forum.kirupa.com/u/nikomax)\
**Post date:** [June 8, 2008, 5:12am UTC](https://forum.kirupa.com/t/variables-in-a-class/262628/1 "2008-06-08T05:12:40Z")

</div>

Hi there,  
I had tried searching this for the last 2 hours =[ so I decided to give in and ask.

I am have a number of variables defined in my document class and I have noticed that if I were to call a function in the same class to edit the value of the variable, the change would only apply to the scope of the function.

Now I hope I used scope in the right context there =D

Here is my code to show what I mean:

```auto

package {
    import flash.display.*;
    import flash.xml.*;
    import flash.events.*;
    import com.xml.xmlLoad;
    import com.gui.mainGui;

    public class main extends MovieClip {

        private var _targetURL:String = "Musicinfo.xml";
        private var getXMLLoad:xmlLoad;
        private var getGui:mainGui;
        public var xmlData:XML;
        public var _lessonInfo:XMLList;
 
        public function main() {
            loadXML(_targetURL);
            processXML();//This will return TypeError: Error #1009: 
                               //Cannot access a property or method of a null object reference.
                                //at main/::processXML()
            trace(xmlData);//This will also return an error similar to above

        }
        function loadXML(_targetURL) {
            getXMLLoad = new xmlLoad(_targetURL);
            getXMLLoad.addEventListener(Event.COMPLETE, onComplete);
        }
        function onComplete(evt:Event) {
            xmlData = getXMLLoad.getXML();
            trace(xmlData.fileInfo.blurb);//This traces the XML data
            trace(xmlData.fileInfo.author);//And so does this
        }
        function processXML() {
            trace(xmlData.fileInfo.blurb);//This dosen't =[
        }
    }
}

```

Could someone explain why this is?  
This may be a AS2 understanding but I thought that if I at changed the value of a variable made in the class, then the value would be the new value for every time the variable was called after the new value was put there.

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 8, 2008, 5:31am UTC](https://forum.kirupa.com/t/variables-in-a-class/262628/2 "2008-06-08T05:31:11Z")

</div>

You can’t access the xmlData variable until you’ve instantiated the variable, or it’s null.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 8, 2008, 6:31am UTC](https://forum.kirupa.com/t/variables-in-a-class/262628/3 "2008-06-08T06:31:36Z")

</div>

Problem is you call processXML before it’s loaded. You should call the function in the onComplete handler.

---

<div class="post-metadata">

**Author:** ![nikomax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nikomax/32/3664_2.png) [@nikomax](https://forum.kirupa.com/u/nikomax)\
**Post date:** [June 8, 2008, 12:13pm UTC](https://forum.kirupa.com/t/variables-in-a-class/262628/4 "2008-06-08T12:13:55Z")

</div>

Ok I probably thought that by the time processXML(); was called the XML would be already loaded…

Does flash process in a liner way? I thought the functions would of been run in this order:

loadXML(); -\> onComplete(); -\> processXML();

Therefore I thought that the value of xmlData would then be the XML file when it came to do the processXML(); function.

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [June 8, 2008, 1:05pm UTC](https://forum.kirupa.com/t/variables-in-a-class/262628/5 "2008-06-08T13:05:38Z")

</div>

no, it will start the processXML() immediately after loadXML() unless you specifically tell it to wait until the loadXML complete event dispatches.

---

<div class="post-metadata">

**Author:** ![nikomax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nikomax/32/3664_2.png) [@nikomax](https://forum.kirupa.com/u/nikomax)\
**Post date:** [June 8, 2008, 3:49pm UTC](https://forum.kirupa.com/t/variables-in-a-class/262628/6 "2008-06-08T15:49:28Z")

</div>

How would I do that?

EDIT::  
Well thinking about it could i just add an even listener?
