Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 16017

Global Variables

$
0
0
I'm still struggling to come to grips with global variables although I've used them a lot in other languages, possibly too much!
I do understand the use of a static class but what's bugging me is various comments that I read online suggesting that global variables are really deprecated in C#. For example one I have read says "C# is an object-oriented language and does not support global variables directly. The solution is to add a static class containing the global variables. Using a global variable violates the OOP concept a bit, but can be very useful in certain circumstances.". It doesn't explain what those circumstances are.
I do understand that.
However I'm interested in how people implement common classes without using a global variable to hold a reference to the class instance, especially when you only want a single instance of the class. For example, if I have a class which is used frequently throughout the life of the program from multiple places (and from multiple classes) the simplest way is to instantiate the class in a global variable and allow everything that needs it to use that reference. I realise that this breaks encapsulation and so is probably not a good way to do it, but how else would people handle it?

Thanks
Ian

Viewing all articles
Browse latest Browse all 16017

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>