Flash
  Home arrow Flash arrow Page 3 - ActionScript Syntax for Flex Applications
IBM developerWorks
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 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

ActionScript Syntax for Flex Applications
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2008-05-08

    Table of Contents:
  • ActionScript Syntax for Flex Applications
  • Declaring Classes
  • Variables and Properties
  • Variables and Properties continued

  • 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

    ActionScript Syntax for Flex Applications - Variables and Properties


    (Page 3 of 4 )

    A variable is a named element you can use to store data or a reference to data. You can assign values to and read values from a variable.

    When you want to work with a variable, the first thing you’ll need to do is declare it. Declaring a variable allocates memory for it and tells the application that the variable exists. You can declare a variable using thevarkeyword as follows:

      var variableName;

    Thevarkeyword is followed by the name of the variable. Variable names in ActionScript are arbitrary, but they must follow a few simple rules:

    • The variable name can consist only of letters, numbers, dollar signs, and underscores. 
    • The variable name must not start with a number.

    By convention, all ActionScript variables use initial lowercase characters rather than initial uppercase characters. The following declares a variable calleduserName:

      var userName;

    Although you can declare a variable without a data type, it’s always recommended that you declare a variable with a data type. You can add a data type to the variable declaration using post-colon syntax as follows:

      var variableName:DataType;

    The data type determines the kind of data you can store in the variable. There are many data types, ranging from simple strings and numbers to reference types (such as arrays), and all the types defined by the Flex framework (e.g.,TextInput,Button, etc.). There are far too many data types to list comprehensively here (especially since you can define custom data types). However, some of the most common core data types areString,Number,int,uint,Boolean,Date, andArray, as defined in Table4-1 .

    Table 4-1. Common data types and their descriptions

    Data type Description
    String

    One or more characters, including all Unicode characters

    Number

    Any numeric value, including floating-point numbers

    int Positive and negative integers and 0
    uint Positive integers and 0
    Boolean True or false
    Date The date and time
    Array An index-ordered collection of data

    When a variable is declared with a data type, you’ll receive errors if you attempt to assign an invalid value to the variable. Flex applications provide both compile-time and runtime type checking.

    The following example declares theuserNamevariable with the data typeString:

      var userName:String;

    Once you’ve declared a variable, the next thing to do is assign values using an assignment operator (an equals sign), as in the following example:

      userName = "Flex User";

    You can also combine a declaration and assignment into one line:

      var userName:String = "Flex User";

    When you want to retrieve a value from a variable, you simply reference the variable in a statement or expression that expects that type of value. The following example assigns the value from theuserNamevariable to thetextproperty of atextinput component:

      textInput.text = userName;

    Variables are placed within class methods (find more on method syntax in the “Methods section, later in this chapter). Variables declared outside of methods are called properties, and they are scoped to the entire class. In most respects, variables and properties are the same. However, there is one key difference that shows up syntactically, which is simply a matter of scope. Here we describe the contrast between variable and property scope:

    1. All variables declared within methods are scoped exclusively to those methods. That means you cannot reference a variable outside the method in which it is declared.
    2. Properties, on the other hand, have much greater scope. At a minimum, a property is accessible within the entire class. However, you can also opt to allow the property to be accessible outside the class with various settings called modifiers.

    Classes define properties using quite a few possible modifiers. A property can be one of the following:

    public 
       Thepublicmodifier means the property is 
       accessible outside the class (e.g., from an instance 
       of the class).

    private
       Theprivate modifier makes the property accessible
       only within the class.

    protected
      Theprotectedmodifier makes the property
       accessible only within the class and its subclasses.

    internal
      Theinternalmodifier makes the property
       accessible only within the package.

    Practically, you should always declare properties asprivateorprotected. It is not a good idea to declarepublicproperties because a class should always manage its state (the values of its properties).internalproperties are a bad idea for the same reason.

    You can declare properties in much the same way as you would declare variables: using thevarkeyword. In addition, a property name must follow the same rules as variable names. A common convention (and one used by this book) namesprivate andprotected properties with an initial underscore (_) to help distinguish them from local variables declared within methods. The following example declares aprivate property called_loader of typeURLLoader:

      package com.example{
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        public class Example {
         
    private var _loader:URLLoader;
        }
      }

    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 2 hosted by Hostway