PDA

View Full Version : [AS3] How to set optional parameters in a custom class



Knorcedger
February 24th, 2007, 02:37 PM
Im coding in AS3, and i want to make a class that will recieve optional parameters, but i dont know how to identify these parameters as optional.

I tried writing

public function mydate([language:String]):void{like Adobe identifies optional parameters, but it doesnt work

Any help will be appreciated :)

senocular
February 24th, 2007, 02:43 PM
use ...rest
http://www.kirupa.com/forum/showthread.php?p=1926071#post1926071

TheCanadian
February 24th, 2007, 06:27 PM
Or, if you know the name of the parameter and want to use a default value if the argument isn't passed, you can assign the default value to the parameter:

public function mydate(language:String = "english"):void{
In this way, the caller doesn't have to give the argument, and the code will use the string english in the code.

Dazzer
February 25th, 2007, 02:03 AM
or if there isn't suposed to be a value, you can just use 'null'

Knorcedger
February 25th, 2007, 06:24 AM
Thank u all guys :)

Slippy
July 6th, 2009, 06:44 PM
I know this doesn't pertain to AS3, but I thought it would help others using search engines to find answers.

In AS2, if you want to create a class that takes 1 or more optional arguments in the constructor then you might run into the 'Type mismatch' error when using that class.

Example:

[defined in Foo.as]
class Foo
{
private var __name :String;
private var __verbose :Boolean;

public function Foo(name:String, verbose:Boolean)
{
if (name == undefined) __name = "Foo_" + Math.floor(Math.random()*100);
if (verbose == undefined) __verbose = false;

trace("name: " + __name);
trace("verbose: " + __verbose);
}
}

[Now in another as file, or swf]
var foo = new Foo(true);

You can see what the problem is. If only 1 argument is passed, then actionscript assumes it's the first argument in the class constructor.
Here's a solution I came up with that might be useful for someone. I don't advise the practice of not data typing, but in simple cases you can use this approach since actionscript 2 (or 3?) doesn't allow you to create multiple class definitions.

class Foo
{
private var __name :String;
private var __verbose :Boolean;

public function Foo(name, verbose)
{
for (var i=0; i<arguments.length; i++)
{
switch (typeof(arguments[i]))
{
case ("boolean"):
__verbose = arguments[i];
break;
case ("string"):
__name = arguments[i];
break;
}
}

if (__verbose == undefined)
__verbose = false; // or whatever you want as default
if (__name == undefined)
__name = "Foo_" + Math.floor(Math.random()*100);

trace("name: " + __name);
trace("verbose: " + __verbose);
}
}

Now when you create a new Foo class you can add 0, 1, or 2 arguments in any order.

var foo = new Foo();
outputs
name: Foo_96
verbose: true

var foo = new Foo(true);
outputs
name: Foo_67
verbose: false

var foo = new Foo("bar");
outputs
name: bar
verbose: false

var foo = new Foo(true, "bar");
outputs
name: bar
verbose: true

var foo = new Foo("bar", true);
outputs
name: bar
verbose: true


The reason I have the switch statement in there is for the chance that someone would need more than one optional argument of the same type. I'm sure there are other ways to do this, but here's a way that works for simple cases and will get you back into develpment and out of the search engines.

xgraves
July 8th, 2009, 03:14 PM
I find that "null" is really handy, as in:
public function item_hilite(evt:Event=null):void {
// some code to hilite an object
// etc
so now I can call that same function with an event listener such as MOUSE_OVER, and also as a function if I just need it to set a state, such as: menu_item.item_hilite();