We help IT Professionals succeed at work.

does AS3 have a variable amount of parameters option?

blue-genie
blue-genie used Ask the Experts™
on
assume i want to create a function that will call different methods in the webservice and pass the required parameters for that method. how would I do this?
 
public function callWSDL(action:String, ....):void
		{
			switch (action)
			{
				case "login":
					WSDLAction  = "login";
					op.something(); 
					break
				case "register":
					WSDLAction  = "register";
					op.somethingelse(); 
					break;
			}
		}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Not sure if this is what you are looking for...
This will call the first method with two arguments when called callWSDL("login", "username", "password"); and call the second method with three arguments when called callWSDL("register", "username", "password", "my@email.com");

CyanBlue
public function callWSDL(action:String, ...rest):void
{
	switch (action)
	{
		case "login":
			WSDLAction  = "login";
			op.something(rest[0], rest[1]); 
			break;
		case "register":
			WSDLAction  = "register";
			op.somethingelse(rest[0], rest[1], rest[2]); 
			break;
	}
}

Open in new window

Another option would be to define optional parameters by appending a default value to the variable declaration:
function optionalParameters(a:Number=null, b:String="default") { }

you could call this function the following ways:

optionalParameters();
optionalParameters(2);
optionalParameters(3, "text");
Cyan specified what you needed.  
Uuups ... sorry ... didn't see that. Must have bin a simoultaneous post.
It would be nice if EE would modify the posting for on screen updates as well as the question list..
But nevertheless my second post offers a second option that also answers the question asked.
Top Expert 2009

Author

Commented:
you guys rock!!!
posting another question now. ... go check it out.

(Genie wishes she paid attention to OOP 101);

Commented:
A funcky one with this syntax is that the optional arguments need to be positioned at the last...
   function optionalParameters(a:Number=null, b:String="default") { }

So, you cannot do this...
   function optionalParameters(a:Number=null, b:String="default", c:String) { }

OOP...  Who cares about that???  The only thing that comes out of my mouth is oops all the time when I try to use OOP...  :p

CyanBlue
Top Expert 2009

Author

Commented:
no cb i've been having a fab day with other peoples classes.
why is this protected? why is that static, no i need that, let me in .......