PDA

View Full Version : Create class without calling constructor



dataxa
April 11th, 2009, 09:54 AM
How to create new class instance without calling constructor function, meanwhile constructor function must exist and is requiring 1 parameter.

var newclass:myclass = new myclass; <- still calls function myclass in myclass class

I would like to call constructor function later:

newclass.newclass(myparam);

senocular
April 11th, 2009, 11:06 AM
when you use the new operator, you're calling the constructor (function named after the class name). There's no getting around that. To have an instance of an object in AS, the constructor must first be called.

Anogar
April 11th, 2009, 11:47 AM
It sounds like what you might want to do is something like this:



package
{
public class MyClass
{
public function MyClass()
{
//remove code
}

public function whateverYourConstructorDid(param:*):void
{
// the constructor code goes here now.
}
}
}It's often a good idea to keep some of your code outside the constructor so that you can easily control when it happens, and it sounds like this is the issue you're running into right now. I personally try to have very little inside my constructor, usually just any required variables, but nothing really interesting.