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;
    }


}

}
3
  • 1
    Arrays in as3 are more like arrays in JavaScript and less like Java. Still, if you want a limited sized, typed array, you can use a typed Vector: var countries:Vector.<Country> = new Vector.<Country>(10,true); Commented Apr 1, 2012 at 3:21
  • Also, in as3 you have getter/setters (somewhat similar to those in C#): public function set name(n:String):void{cName = n} public function get name():String{return cName;}//then with an instance: country.name = "A";//setter trace(country.name);//getter although, using java style like what you now is a bit faster I think. Commented Apr 1, 2012 at 3:33
  • This 'new Country(10)' is not array nor in C++, nor in Java !! Commented Apr 1, 2012 at 12:01

2 Answers 2

2

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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1. Consider using Vector.<Country> instead of Array for AS3 - help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/…
0

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()]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.