Jan 25 2009

AS3 How to build an object getter function

Category: ClassesHadi Tavakoli @ 8:56 pm

I was in the process of completing my DropDownMenu class and I needed a way to save the information of the selected item in the list and the best thing would be to use the getter functions. In my later posts you'll see the dropDownMenu but for right now to give you an idea, we would need the following details to be accessible when an item is selected:

lable, detail, thumb, data and index.

So, if we used the getter functions normally, I had to create something like below 5 times!

public function get index():int
{
return _index;
}

It may work, but we can do something better, to save the data all in an object and use only one getter function and extract the kind of details we'd like to see.

In order to do so, you should do the following in your Class file. First introduce your object with all of the information you want to save in it, easily like this:

private var _selectedItem:Object = {lable:"EMPTY", detail:"EMPTY", thumb:"EMPTY", data:"EMPTY", index:0};

it will be very easy to overwrite the values of an object, as an example, you are able to set values to such an object like this:

_selectedItem.data = "some data here!";

ok, and after you have the object ready you should write your getter function like this:

public function get selectedItem():Object
{
return _selectedItem;
}

easy right? :) you don't have to write several getters now and you will call the getter function from outside of the class like this:

trace(the Class.selectedItem.index);
trace(myCombo.selectedItem.lable);
trace(myCombo.selectedItem.detail);
trace(myCombo.selectedItem.thumb);
trace(myCombo.selectedItem.data);

That's it, I hope you like this idea.

Regards,
Hadi

Tags: , , ,

2 Responses to “AS3 How to build an object getter function”

  1. admin says:

    ok, the example above does the same thing, doesn’t it? it will save data in an object and we can call the getter function and the specific item in the object…

  2. Hadi Tavakoli says:

    get more here at our new website: http://www.myflashlab.com

Leave a Reply

*