| By Andrew Trice | Article Rating: |
|
| February 6, 2007 10:00 AM EST | Reads: |
13,572 |
Andrew Trice, co-author of Flex 2 articles on Adobe's Developer Center, maintains a blog at http://www.cynergysystems.com/blogs/page/andrewtrice in which he helps orient newbies and experts alike about Flex. Web Developer's & Designer's Journal is pleased here to bring a sampler of some of his more recent shared insights.
Getters and Setters vs. Public Properties in Flex
I've been asked several times, why would you use get/set functions instead of public variables in your Flex components and classes? Well, there are some great things you can do with getters and setters that you can't do with public variables. On the other hand, there are cases where public variables work better. When using these functions and/or public variables, the code for the caller will be the same:
mycomponent.myValue = 1;
First, let's look at public variables...
[Bindable]
public var myValue : Number
It is better to use public variables when there are no additional actions that need to take place when the value has been changed. If you change the value of "myValue", the bindings will update and everything will be handled accordingly. The value will change, and anything bound to that value will change. In this case, there is no need to use getter/setter methods.
Now, on to getters and setters...
[Bindable(event="myValueUpdated")]
public function set myValue (value:Number):void
{
_myValue = value;
dispatchEvent( new FlexEvent( "myValueUpdated" ) );
}
public function get myValue ():Number
{
return _myValue ;
}
private var _myValue : Number;
First I'll explain the [Bindable(event="myValueUpdated")] statement: This indicates that the data binding to the getter's value should be updated when the event of type "myValueUpdated" is dispatched. You'll notice that when the value is set, this event is dispatched, which would notify any components that are bound to this value.
Now, the rest... The code that I showed above doesn't have any benefits over the public property; it functions in exactly the same way, but requires more code. The benefit of getter and setter functions is that they enable sequential code execution when the value is changed. You can create your components so that specific functions are executed any time that the value is accessed using get and/or set functions.
Here's an example:
[Bindable(event="myValueUpdated")]
public function set myValue (value:Number):void
{
_myValue = value;
numSets ++;
myFunction();
dispatchEvent( new FlexEvent( "myValueUpdated" ) );
}
public function get myValue ():Number
{
numGets ++;
myOtherFunction();
return _myValue ;
}
private var _myValue : Number;
private var numGets : Number = 0;
private var numSets : Number = 0;
In this example, every time the value is set, the numSets Number is incremented, and the myFunction() function is executed. Likewise, every time the value is accessed using the "get" method, the numGets Number is incremented, and the myOtherFunction() function is executed. There is no limit to what kind of code you can execute here. You can have it dispatch custom events, change styles, create new components, etc. This turns out to be very handy when creating custom Flex components.
Published February 6, 2007 Reads 13,572
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About Andrew Trice
Andrew Trice is a consultant with Cynergy Systems in Washington, DC, where he specializes in development of Flex-based Rich Internet Applications. Andrew has over 5 years of proven experience in the RIA industry, including application design and development using Flex, Flash, ColdFusion, J2EE and .NET architectures.
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Ulitzer’s Amazing First 30 Days in Public Beta
- "Government IT Expo" to Highlight Cloud Computing and SOA
- Will Ulitzer Dominate News Content on The Web? -Gartner
- Clear Toolkit 4: The Road Map
- Creating Adobe AIR Native Menu with Flash CS4
- Ulitzer Responds to Published Reports
- Ulitzer vs. Ning - a Quick Review
- Adobe AIR: Creating Dock and System Tray Icon Menus
- Social Media Terrorists
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Web Services Using ColdFusion and Apache CXF
- Adobe Takes LiveCycle into the Cloud
- Ulitzer’s Amazing First 30 Days in Public Beta
- Adobe Creates a Sandbox in the Sky
- "Government IT Expo" to Highlight Cloud Computing and SOA
- Will Ulitzer Dominate News Content on The Web? -Gartner
- The Role of an RIA in the Enterprise
- Clear Toolkit 4: The Road Map
- Creating Adobe AIR Native Menu with Flash CS4
- The Next Programming Models, RIAs and Composite Applications
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Building a Zip Code Proximity Search with ColdFusion
- Web Services Using ColdFusion and Apache CXF







































