user784637
user784637

Reputation: 16102

What does 1071: Syntax error indicate and how do I resolve this code?

I'm going through tutorials on dynamic classes. The concept is really exciting and interesting to me.

However this code isn't compiling correctly

dynamic class Person {
var name:String;
}

Person p= new Person();
p.name=”Joe”;
p.age=25;
p.printMe = function () {
trace (p.name, p.age);
}
p.printMe(); // Joe 25

I get a 1071 syntax error.

What gives?

Upvotes: 1

Views: 512

Answers (2)

Gerhard Schlager
Gerhard Schlager

Reputation: 3145

I can't test it right now, but it looks like there are two errors in your code snippet. First, the variable declaration and second the string quotes (you used instead of "). The following code should work:

var p:Person = new Person();
p.name = "Joe";
p.age = 25;
p.printMe = function() {
    trace (p.name, p.age);
}
p.printMe(); // Joe 25

Upvotes: 1

Jonatan Hedborg
Jonatan Hedborg

Reputation: 4432

Error in syntax; Person p = new Person(); is not valid AS3. It should be var p:Person = new Person();"

EDIT 1: Also, of course if you put your code as-is in the timeline it will not work. The class has to be in a .as file, and the other code must be in the timeline (or in a class function).

EDIT 2: This code works:

//Timeline:
var p:Person = new Person();
p.name="Joe";
p.age=25;
p.printMe = function () {
    trace (p.name, p.age);
}
p.printMe(); // Joe 25`

Where Person.as looks like:

package {
    public dynamic class Person {
        var name:String;
    }
}

Upvotes: 4

Related Questions