user3090763
user3090763

Reputation: 1099

Can we make a public var variable that is accessible to all functions?

I want to make a var variable that is accessible across all functions ? How can I make it I tried public var a but it gives error?

Upvotes: 0

Views: 84

Answers (3)

ke4ktz
ke4ktz

Reputation: 1312

If you want to have the variable accessible across all methods in your call, you'll have to move it out of the method it's in and move it up to the class level. You can make in private to prevent other classes from getting to it, but you can't have a method variable available outside of that method.

Upvotes: 0

phoog
phoog

Reputation: 43056

The var keyword is only available for local variables. Class-level fields must have their types explicitly declared.

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180858

The C# language is designed to sequester all such state variables inside of a class.

What you're probably looking for is a Singleton. Whether that's a good idea or not is an open question.

Upvotes: 3

Related Questions