Home arrow Flash arrow Page 3 - ActionScript Syntax for Flex Applications
FLASH

ActionScript Syntax for Flex Applications


In this second part of a five-part series that focuses on using ActionScript in Flex applications, you will learn ActionScript's syntax and how to use it with Flex. 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: 4 stars4 stars4 stars4 stars4 stars / 8
May 08, 2008
TABLE OF CONTENTS:
  1. · ActionScript Syntax for Flex Applications
  2. · Declaring Classes
  3. · Variables and Properties
  4. · Variables and Properties continued

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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;
    }
  }


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

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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