davegallant
davegallant

Reputation: 533

How to instantiate an array of custom classes in Action Script 3.0

I'm new to AS3 and I'm getting this error while trying to implement OO style code.

  Incorrect number of arguments.  Expected no more than 0.

When I try to:

var countries:Country = new Country(10);

Normally this would work in Java or C++, so I'm not sure what's up!?

Here is my custom class.

package {

public class Country {

    var cName:String = "noName"; 
    public function Country() {
        // constructor code
    }

    public function setName(n:String):void {
        cName = n;
    }
    public function getName():String {
        return cName;
    }


}

}

Upvotes: 0

Views: 1475

Answers (2)

liupeixin
liupeixin

Reputation: 738

your constructor function public function Country() {} not have an argument, but you give 10, must go wrong.

ActionScript's array not like c++, don't need element type <Country>

you want to save class in array is simple: var arr:Array = [new Country()]

Upvotes: 0

sberry
sberry

Reputation: 132018

You are passing 10 to the constructor, which is not what you want to do. To instantiate an array of instances, try something like this:

var countries:Array = []
var country:Country;
for (var i:uint = 0; i < 10; i++) {
    country = new Country()
    country.setName("Country_" + i);
    countries.push(country)
}

Upvotes: 2

Related Questions