YOUR FEEDBACK
Virtualization Viewpoint: Cloud Computing Casts Shadow on Walled Gardens
Neil Mansilla wrote: I've been working and deploying applications on the W...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
MXDJ TOP LINKS YOU MUST CLICK ON !


Ask the Expert
Basics of Flex explained

Digg This!

Page 1 of 4   next page »

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.



Page 1 of 4   next page »

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.

LATEST FLEX STORIES & POSTS
Two great PDF creators
I like reading stuff in pdf format. But it's even better if you can easily create pdf files. By easily I mean a button click. Literally.Since I have Adobe Acrobat, my Microsoft Word and PowerPoint just have an extra menu to create it. But it's kinda boring. Let me share with you a cou
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
A Runtime Integration Approach to Application Development
This pattern is a hybrid of plug-in and event-driven architecture to integrate individual plug-ins together to come up with the Plug-in Integrator Pattern. This pattern leverages the benefits of both these well-known architectures to provide an optimal solution to build an enterprise-r
JavaOne 2008: Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-the-party - Adobe AIR- and Microsoft Silverlight-competitive JavaFX Rich Client environment as a potential revenue-generator capable of putting ads on mobile applications and JavaFX Scri
Facelift Your SOA with Rich Internet Applications
We are entering an era of Rich Internet Applications (RIA) and enhancing the user experience of consumers of the services becomes an important part in designing and implementing SOA. But if you decide to develop rich clients, you'll be facing the dilemma - which way to go - remain with
Adobe Flash Player 10 Public Beta Now on Adobe Labs
Today, Adobe announced the immediate availability of Adobe Flash Player 10 beta as a free download from Adobe Labs. Adobe Flash Player 10 beta, code named 'Astro', builds on the capabilities of the world's most ubiquitous application runtime with new support for custom filters and effe
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE