3

Hi I am new to Typescript and I need to use static array in my code. But when I declared array as follows

private static arrayname:String[];

When I typed this. it didn't show me the array name so I can't able to push any values into this array. Can any one help me in this?

1
  • If you want to use this for a static variable then I guess you should start thinking about reading the basics again.... Anyway String in typescript is normally low-case Commented Jun 23, 2016 at 11:27

1 Answer 1

7

You should use class name instead of "this":

class A {
    private static arrayname: string[] = ["a", "b", "c"];

    someFunc(): number {
        return A.arrayname.length; // Here I'm calling private statis property
    }
}


var instanceA = new A();
alert(instanceA.someFunc());

This code works for me in the Typescript playground.

Note

You can use private member inside class functions only. It isn't accessible and visible outside the class declared in.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried that too my class name is Sample but when I tried Sample. it doesn't shows the array name
now I can push data but I want it to use it outside the class hence I have changed to public but the array is not visible outside
Public members defenitely are visible from outside the class. The problem can be if classes are declared in different namespaces.

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.