Flash
  Home arrow Flash arrow Manipulating Data with ActionScript in Fle...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Manipulating Data with ActionScript in Flex Applications
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-05-15

    Table of Contents:
  • Manipulating Data with ActionScript in Flex Applications
  • Expressions
  • Arrays
  • Objects
  • Inheritance

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Manipulating Data with ActionScript in Flex Applications


    (Page 1 of 5 )

    In this third part of a five-part series focused on using Flex and ActionScript together, you will learn about methods, arrays, and more. This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Methods

    A method is a way to group together statements, give that group a name, and defer the execution of those statements until the method is called by its name. All method definitions must be placed within a class body, and they use the function keyword followed by the name of the method. Following the method name is a pair of parentheses enclosing any parameters that the method might accept. That is followed by a colon and the return type of the method. If the function does not return a value, the return type is declared as void. Following the return type declaration is the function definition enclosed in opening and closing curly braces. The following is a declaration for a function calledtest():

      function test():void{
      }

    Thetest()method is declared so that it does not expect any parameters, and it does not expect to return a value. Currently, thetest()method doesn’t do anything either. Next, add a few statements inside thefunctionso that it does something:

      function test():void {
        var message:String = "function message";
        trace(message);
     
    }

    Thetrace()function writes text to an output such as a console or logfile. Chapter 17 discussestrace()in more detail.

    Now thetest()method declares a variable calledmessage, assigns a value to it (function message), and then usestrace()to output the value to the console (if debugging).

    To call a method, use the method name followed by thefunctioncall operator (the parentheses). For example, if you want to call thetest()method, you would use the following statement:

      test();

    If you want to declare a method so that you can pass it parameters, you must declare the parameters within the parentheses as a comma-delimited list. The parameter declarations consist of the parameter name and post-colon data typing. The following example rewritestest()so that it expects two parameters (aandb):

      function test(a:String, b:String):void {
       
    trace("Your message is " + a + " and " + b);
      }

    When you want to call a method with parameters, simply pass the values within the function call operator, as in the following example:

      test("one", "two");

    ActionScript does not allow overloading. That means you cannot have two methods with the same name but different signatures (different parameter lists). However, ActionScript does allow for rest parameters. Rest parameters allow you to pass zero or more additional parameters of unknown types to a function. You declare a rest parameter using a parameter name preceded immediately by three dots. Within the method you can access the rest parameter values as an array.

    Currently, thetest()example requires exactly two parameters (aandb). You cannot pass fewer or more than two parameters. If you want to pass just one parameter (or five parameters), you need a solution that rest parameters provide. The following code rewritestest()so that it always requires at least one parameter, but it also allows for zero or more additional parameters. By convention, the rest parameter is calledrest(though you may use arbitrary names for the parameter):

      function test(a:String, ...rest):void{
        var message:String = "Your message is";
        for(var i:uint = 0; i < rest.length; i++) {
         
    message += " " + rest[i];
        }
        trace(message);
      }

    If you want to return a value from a method you need to do two things: specify the correct return type, and add areturnstatement. When you specify a return type, you’ll get both compile-time and runtime checking. A function set to return aStringvalue must return a string, not a number, date, array, or any other type. Areturnstatement immediately exits the function and returns the specified value to the expression or statement from which the function was called. The following rewrite oftest()returns a string:

      function test(a:String, ...rest):String {
        var message:String = "Your message is";
        for(var i:uint = 0; i < rest.length; i++) {
          message += " " + rest[i];
        }
        return message;
      }

    Methods use the samepublic,private,protected,internal, andstatic modifiers as properties. If you omit the modifiers (as in the preceding examples), Flex assumes the methods areinternal. The following declares two methods, onepublicand onepublicandstatic:

      package com.example {
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        public class Example {
         
    private var _loader:URLLoader;
          static private var _instance:Example;
          static public const TEST:String = "test constant";
         
    public function traceMessage(message:String):void {
           
    trace("Your message is " + message)
    :
          }
          static public function getInstance():Example {
           
    if(_instance == null) {
             
    _instance = new Example();
            }
            return _instance;
         
    }
       
    }
      }

    Unlike properties, it is common and acceptable to declare public methods.

    Classes also can and should have a special type of method called a constructor. The constructor method has the following rules:

    1. The method name must be the same as that of the class.
    2. The method must be declared aspublic.
    3. The method must not declare a return type or return a value.

    The following constructor assigns a new value to the_loaderproperty:

      package com.example {
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        public class Example {
         
    private var _loader:URLLoader;
          static private var _instance:Example;
          static public const TEST:String = "test constant";
          public function Example() {
            _loader = new URLLoader();
          }
         
    public function traceMessage(message:String):void {
           
    trace("Your message is " + message);
          }
          static public function getInstance():Example {
           
    if(_instance == null) {
             
    _instance = new Example();
            }
            return _instance;
         
    }
        }
      }

    There are two additional special method types called: implicit getter and setter methods. These are declared as methods, but they are accessible as though they werepublic properties. The method declarations are identical to normal method declarations, except for the following:

    1. Getter methods use thegetkeyword.
    2. Setter methods use theset keyword.
    3. Getter methods must not expect any parameters and must return a value.
    4. Setter methods must expect exactly one parameter and must be declared with avoidreturn type.

    The following example declares a getter and a setter method, each calledsampleProperty. In this example, a newprivateproperty is declared using the getter and setter methods as accessors. This is not a requirement for getter and setter methods, but it is a common use case:

      package com.example{
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        public class Example {
         
    private var _loader:URLLoader;
          static private var _instance:Example;
          private var _sampleProperty:String;
          public function get sampleProperty():String {
           
    return _sampleProperty;
          }
          public function set sampleProperty(value:String):void {
            _sampleProperty = value;
          }
          static public const TEST:String = "test constant";
          public function Example() {
            
    _loader = new URLLoader();
          }
          public function traceMessage(message:String):void {
            
    trace("Your message is " + message);
          }
          static public function getInstance():Example {
           
    if(_instance == null) {
             
    _instance = new Example();
            }
            return _instance;
          }
        }
      }

    You can call the getter method by using the method name as a property in a context that attempts to read the value. You can call the setter method by using the method name as a property in a context that attempts to write a value. The following example creates an instance of theExampleclass, then writes and reads a value to and from the instance using the getter and setter methods:

      var example:Example = new Example();
      example.sampleProperty = "A";   // Call the setter, passing it A as a parameter
      trace(example.sampleProperty);  // Call the getter

    More Flash Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming Flex 2," published by...
     

    Buy this book now. This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.

    FLASH ARTICLES

    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks
    - Building Preloaders
    - Fun Things to Do with Movie Clips in Flash MX
    - Referencing Movie Clips in Flash MX







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway