Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-06-2007, 08:05 PM   #1
EatJason
Registered User
 
Join Date: Nov 2007
Posts: 8
Default Actionscript Constructor Overloading

Is it not possible to overload constructors in actionscript? I just want two different ways to construct a class. Meaning I want two different parameter lists, not simply omiting certain variables. Do I have to just make a single constructor with all parameters, and run a function based on which ones are filled in?
EatJason is offline   Reply With Quote
Old 12-06-2007, 08:31 PM   #2
panel
AS3
 
panel's Avatar
 
Join Date: Mar 2007
Location: Warsaw
Posts: 1,991
Send a message via Skype™ to panel
Default

No overload is possible in AS3
panel is offline   Reply With Quote
Old 12-06-2007, 10:11 PM   #3
Nomad2000
ActionScripter/Flasher
 
Join Date: Nov 2007
Location: Vancouver, Canada
Posts: 159
Default

You can have default parameters in the constructor, which let you call it with different numbers of parameters:

Code:
class Foo
{
    function Foo(x:int = 0, y:int = 0):void
    {
        // Do something with x and y
    }
}
Or you could take the approach of using initialize() methods:

Code:
class Foo
{
    function Foo():void
    {
    }

    function initialize(x:int = 0, y:int = 0):void
    {
        // Initialize the object using x and y
    }

    function initializeString(s:String):void
    {
        // Initialize object by parsing the string
    }
}

// To use it
var foo1:Foo = new Foo();
foo1.initialize(1, 2);
var foo2:Foo = new Foo();
foo2.initialize("1,2");

Last edited by Nomad2000; 12-06-2007 at 10:26 PM.
Nomad2000 is offline   Reply With Quote
Old 12-07-2007, 12:03 PM   #4
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 914
Default

Or if the parameterlist is substantially different you can use the rest parameter
ActionScript Code:
package{     class Foo{         public function Foo( ...args ){             if( args[ 0 ] is Date ){                 _init( args[ 0 ] );             }else if( args[ 0 ] is String ){                 var d : Date = new Date( args[ 2 ], args[ 1 ], args[ 0 ] );                 _init( d );             }         }                 private function _init( date : Date ) : void{             //do something         }     } }

you can use it like this:
ActionScript Code:
var bar : Foo = new Foo( new Date() ); //OR var bar : Foo = new Foo( "07", "12", "2007" );
creynders is offline   Reply With Quote
Old 12-07-2007, 04:23 PM   #5
Nomad2000
ActionScripter/Flasher
 
Join Date: Nov 2007
Location: Vancouver, Canada
Posts: 159
Default

Yes, that makes for a nice clean looking syntax for the caller, but just remember that you lose all compile-time type safety if that is important to you. The compiler cannot tell you that the following code won't work:

Code:
var bar : Foo = new Foo( 7, 12, 2007 );
In this case, the code will fail silently with no error. In other cases if you are not careful, it will crash.

Also, you won't get any syntax completion help with that either.

Check your needs and pick your poison!
Nomad2000 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm beginner in actionscript. I have questions. helgso ActionScript 1.0 (and below) 7 06-16-2006 04:11 PM
Flash Actionscript Career Advice? bdemil General Chat 3 06-15-2006 06:35 PM
Need Help! displaying external text files Flash_Boi ActionScript 2.0 7 03-30-2006 08:23 PM
ActionScript Confusion stanonbass ActionScript 1.0 (and below) 7 12-07-2004 08:16 PM
Actionscript book cheezisgoooood ActionScript 1.0 (and below) 2 02-06-2003 11:31 PM


All times are GMT. The time now is 07:52 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.