Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
  1. #1
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    lets assume that:

    Code:
    var myXML:XML = new XML();
    contains some sort of XML data.

    Now i make a function

    Code:
    function retrnXML():XML
    {
    
    return myXML;
    }
    and call that function:

    Code:
    function callFunc():void
    {
    
    retrnXML();
    }

    it is my understanding that retrnXML() retuns the content of myXML to function callFunc()... but.... how do I access that data via callFunc()?????



    At the same time... what about associative arrays?

    Code:
    var myArray:Array = new Array();
    
    function populateArray():Array
    {
    
    myArray["AL"] = "blah blah"; myArray["AK"] = "yatta yatta"; myArray["PA"] = "yadda yadda" return myArray;
    } function getArray():void {
    populateArray();
    }
    now getArray() has the value of myArray... but how do I access it?

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Just assign it to another variable. For the array example:

    Code:
    function getArray():void
    {
        var populatedArray:Array = populateArray();
    
        populatedArray.forEach(function(item:String, ...args) { /* code */ });
    }
    Also, keep in mind that what actually gets returned is a reference to the original object. This is important because it means that you can modify the original object. So, if I say:

    Code:
    function getArray():void
    {
        var populatedArray:Array = populateArray();
    
        trace(populatedArray["GA"]);
    
        populatedArray["GA"] = "muahahaha";
    }
    The first time I called getArray(), the trace would print out null, because the array's GA property has not yet been set. But the second time I called it, it would say "muahahaha" because the first call did set that property.

  4. #3
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    Quote Originally Posted by Shadowfiend
    Just assign it to another variable.
    so the other question is... MUST I assign it to a variable? Is there another way to work with the data? For example, moving XML data from one variable to another and then using the latter to populate an array... is there a better way to do it?

    Quote Originally Posted by Shadowfiend
    populatedArray.forEach(function(item:String, ...args) { /* code */ });
    HUH??? I undersatnd there is a forEach loop... but am not sure of the forEach(function(item:String, ...args).... etc stuff....


    Quote Originally Posted by Shadowfiend
    Also, keep in mind that what actually gets returned is a reference to the original object. This is important because it means that you can modify the original object. So, if I say:
    but if I modify the original object, that won't modify the object with the reference and new value right?

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Ignore the forEach part -- that's the functional way of iterating over an array, but it isn't important if you don't understand it hehe. The point was to illustrate that you could use the array.

    You can use the return value directly:

    Code:
    for each (var item:String in populateArray())
    {
    }
    
    populateArrayWith(xmlReader.getData())
    This way you avoid an intermediate variable.

    I'm not sure what your last question means. Generally speaking, if something modifies the same instance of an object, then it will modify both the one you are seeing in your function and the one that is being returned from the original function. If the one being returned from the original function is different each time (i.e., if it is instantiated in that function), then you have nothing to worry about. This is basically like saying that if I modify an array that is an instance variable of some class, the class itself will also see those changes.

  6. #5
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    Quote Originally Posted by Shadowfiend
    I'm not sure what your last question means. Generally speaking, if something modifies the same instance of an object, then it will modify both the one you are seeing in your function and the one that is being returned from the original function. If the one being returned from the original function is different each time (i.e., if it is instantiated in that function), then you have nothing to worry about. This is basically like saying that if I modify an array that is an instance variable of some class, the class itself will also see those changes.
    so... if...

    Code:
    var myArray:Array = new Array();
    
    function populateArray():Array
    {
    
    
    myArray["AL"] = "blah blah"; myArray["AK"] = "yatta yatta"; myArray["PA"] = "yadda yadda" return myArray;
    }
    THEN:

    Code:
    function getArray():void
    {
    
    
    var aNewArray:Array = populateArray(); trace (aNewArray["AL"]);
    // blah blah
    }

    but... if....

    Code:
    myArray["AL"] = "dah dah dah";
    
    trace(myArray["AL"]); 
    //dah dah dah

    ...then aNewArray will still be...
    Code:
    trace(aNewArray["AL"]);
    //blah blah


    ******************* and the last Question ******************

    what about a return Boolean, for example:

    Code:
    function retBool():Boolean
    {
    
    return true;
    }
    ... and if i call it....
    Code:
    function callBool():void
    {
    
    retBool();
    }

    than what can I do with that Boolean?

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    In the first part, all of the arrays refer to the same object, so they will all see the same values at all times.

    Booleans are pretty limited, but you can use the function as part of an if statement (e.g., [minicode]if (isOpen(file))[/minicode] where isOpen returns a Boolean).

  8. #7
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    Quote Originally Posted by Shadowfiend
    In the first part, all of the arrays refer to the same object, so they will all see the same values at all times.

    Booleans are pretty limited, but you can use the function as part of an if statement (e.g., [minicode]if (isOpen(file))[/minicode] where isOpen returns a Boolean).

    i see... so by

    Code:
    if (isOpen(file))
    {
    
    }
    you mean
    Code:
    if (isOpen(file = true))
    {
    
    }

    right?

  9. #8
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Close. More like [minicode]if (isOpen(file) == true)[/minicode].

  10. #9
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    ahh right... so you are basically saying "is isOpen(file) true"... that's kind of what i meant... hehe

  11. #10
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Right. Not putting in the `== true' for Booleans is often recommended in style guides. This is because it helps your code read more clearly, and because it encourages naming your methods in such a way that they will naturally fit within an if statement.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 07:19 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com