Hi Adrian,
 I'd do it like this:

private function myMethod(i:Object):Void
{
 if (i instanceof String) {
   // do this
 }
 else if (i instanceof Number) {
   // do this
 }
 else
 {
   // report an error
 }
}

And, as you see, stick in an error condition - because by changing the
input type to Object you throw away all type-safety and can't
guarantee that someone won't try passing in a completely different
type of object...

As to whether it's dirty or not - that's your call, really. :-) It
really depends on the purpose of the function. I've used it
occasionally to fake overloading of functions just to make APIs easier
to understand.

Sorry to be blurry about it, but without knowing a bit more about the
circumstances, it's kind of hard to say...

HTH,
 Ian

On 6/15/06, Adrian Park <[EMAIL PROTECTED]> wrote:
Hi List,

What's the neatest way of accepting a single parameter with 2 possible types
into a method and then working out what type of parameter has been passed?

e.g. in pseudo code

private function myMethod( i:Number/String ):Void {

   if ( i is a String ) {
      // do this
   } else if ( i is a Number ) {
      // do this
   }
}

I can think of several alternatives :
- passing a generic object which contains the property and then using typeof
on that property
- calling 2 different methods but, in this case, it just makes sense to be
one since it's doing the same thing with either parameter (retrieving a bit
of data from an Array which may be identified using a numerical id or a
String ID)
- limiting my method to accepting a String only and then defining a second
method that returns the correspoding String identifier given a numerical
identifier

All of the ways I can think of seem dirty. Is there a nice clean way or is
it wrong to expect the method to accept one parameter of different types?

Thanks in advance,
Adrian P.
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to