Hi all. Thanks for the responses.

I think my favoured route would be by Typing the parameter as generic Object
as it means it's still one parameter which seems neater.

Ian, I'm intrigued if your suggestion works - I had considered it but
assumed it wouldn't work because the result of the typeOf operation would
just be 'object' (since that is what the parameter is typed as).

I'm going to try it as that is exactly the kind of solution I was after.

Thanks
Adrian P


On 6/15/06, Ian Thomas <[EMAIL PROTECTED]> wrote:

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

_______________________________________________
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