Home arrow Flash arrow Manipulating Data with ActionScript in Flex Applications
FLASH

Manipulating Data with ActionScript in Flex Applications


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.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
May 15, 2008
TABLE OF CONTENTS:
  1. · Manipulating Data with ActionScript in Flex Applications
  2. · Expressions
  3. · Arrays
  4. · Objects
  5. · Inheritance

print this article
SEARCH DEVARTICLES

Manipulating Data with ActionScript in Flex Applications
(Page 1 of 5 )

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


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials