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?
Asked
Active
Viewed 79 times
3 Answers
3
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.

Community
- 1
- 1

Robert Harvey
- 178,213
- 47
- 333
- 501
-
Depending on the case a `Static` class would also be acceptable (simpler to set up than a `Singleton`) – Caleb May 16 '14 at 21:33
-
Yeah, I was just discussing that with some folks in the Programmers chat room. – Robert Harvey May 16 '14 at 21:35
0
The var
keyword is only available for local variables. Class-level fields must have their types explicitly declared.

phoog
- 42,068
- 6
- 79
- 117
0
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.

ke4ktz
- 1,152
- 7
- 18