Hello, and best practice question

0 views
Skip to first unread message

paron

unread,
Apr 8, 2009, 7:34:24 AM4/8/09
to Cleveland Adobe Flex Users Group
Hello, my name is Ron Phillips. I work for the Summit County
Engineer's office. I wrote AS back at the turn of the century, but
then got away from it while I pursued RubyOnRails. So, I am a Flex
noob.

I've made a custom component based on ArrayCollection. It is to store
a collection of "DaySlice" Value Objects. I need a special behavior
when a new DaySlice is added, so I wrap setItemAt() in a public
function called "setSliceAt()."

Now setItemAt() is a liability in my custom ArrayCollection -- it
could be used to work around the behaviors written into setSliceAt().

What's the best way of handling it? Disable "setItemAt()" or override
it with a function that just returns null, or override it so it just
returns the object at that index, or what?

Ron

Bachman, Scott

unread,
Apr 8, 2009, 7:49:42 AM4/8/09
to cle...@googlegroups.com
Why not just override setItemAt() and augment it with the new behavior
instead of creating a new method? This way it follows normal conventions
and you can throw an error if, for instance, you don't get a DaySlice
object or do something else if it's a different type of object.

override public function setItemAt(
item:Object, index:int):Object
{
if (item is DaySlice) {
// do something special for dayslice
} else {
throw new Error("Expected a day slice
object.");
}
// if you leave out the else,
// then all other objects will just get normal
behavior
// versus throwing an error
super.setItemAt(item, index);
}

To "disable" the existing method would be going against OOP design
practices for reusability and extensibility not to mention confusing
anyone else who may try to use your class (affecting readability and
maintainability).

Scott

paron

unread,
Apr 8, 2009, 9:48:27 AM4/8/09
to Cleveland Adobe Flex Users Group
Thanks, that is better. I had tried to do:

override public function setItemAt(item:DaySlice, index:int):DaySlice

but, of course, got the "Illegal Override Error". Once I found out I
couldn't do that, I was stuck.
I knew there had to be a recommended way, but I couldn't find an
example, probably because I didn't know which search terms to enter.

Thanks again for getting me unstuck!

Ron

Charles Havranec

unread,
Apr 8, 2009, 9:56:24 AM4/8/09
to cle...@googlegroups.com
think you have to keep the signature the same - e.g. keep return type the same as the underlying class (not :DaySlice)

 
Charles Havranek, PMP
Brecksville, OH
H:
440-526-6558
C:216-854-7158




From: paron <rphill...@gmail.com>
To: Cleveland Adobe Flex Users Group <cle...@googlegroups.com>
Sent: Wednesday, April 8, 2009 9:48:27 AM
Subject: Re: Hello, and best practice question
Reply all
Reply to author
Forward
0 new messages