David Thielen
David Thielen

Reputation: 33026

How do I declare a variable and set the value in a class

I think this code should be right. But it's not. Why?

export module Menu {
    export class FileHandler {

        var infoDisabled : boolean = false;

        isInfoDisabled() : boolean {
            return infoDisabled;
        }

What do I have wrong in this? (I've tried a lot of variations, none worked.)

Upvotes: 1

Views: 3501

Answers (2)

Fenton
Fenton

Reputation: 251242

Two quick changes... Mark the property private (or public if you like) and access it using this.

export class FileHandler {

    private infoDisabled : boolean = false;

    isInfoDisabled() : boolean {
        return this.infoDisabled;
    }

Upvotes: 3

Alex Dresko
Alex Dresko

Reputation: 5213

You were close. :)

export module Menu {
    export class FileHandler {

        infoDisabled : boolean = false;

        isInfoDisabled() : boolean {
            return this.infoDisabled;
        }
    }
}

You cannot define variables at the class level, only properties and methods. Accessing infoDisabled requires the 'this' keyword. And you were missing one or two closing braces. :)

Upvotes: 3

Related Questions