7

Ok after reading this article and also some of the examples I am still not clear on what global variables mean. So its say private variables in your class are global?

http://www.c2.com/cgi/wiki?GlobalVariablesAreBad

So can someone explain this to me in simple terms the context. Does this mean even private fields at the top of your class? I'm not clear on the definition of a "global variable" so that I can distinguish if I'm doing something "bad" in my classes.

PositiveGuy
  • 46,620
  • 110
  • 305
  • 471

6 Answers6

12

In C# an example of a global variable would be a public static variable on a public class. The entire program could read/write to it and it would be shared across threads as well.

thelsdj
  • 9,034
  • 10
  • 45
  • 58
  • 2
    Thanks I guess I was thinking private variables are global in that any method in your class can access it but that's standard OOP and the private variables are still encapsulated in your classes...so yea I see now. – PositiveGuy Jul 22 '09 at 04:14
  • I can't even imagine why one would even want to create a public variable anyway. The thought never crossed my mind and I'm fairly intermediate to architecture of classes. – PositiveGuy Jul 22 '09 at 04:33
  • State variables usually. Configuration. It's generally not a great idea, but it's usually alot easier to code (in terms of getting something functional working) – Matthew Scharley Jul 22 '09 at 04:47
  • Our ASP.net site uses a bunch of public static variables for configuration. Though most of them have getters and setters that do to-disk persistence. Not saying this is a good idea, but making the point that this sort of thing happens in the wild. – thelsdj Jul 22 '09 at 05:15
  • It's alot more common on web systems I think. I don't know if this is just how it is, or if there's something intrinsic to websystems that make it more suitable... I find I use globals alot in PHP, but almost never in desktop C#, perhaps one public static property with just a getter in Program. – Matthew Scharley Jul 22 '09 at 06:54
6

A private field in your class would generally be called a "class level variable". It isn't global to your application because only your class has access to it.

A global variable is a variable that anything in your program can access no matter what scope it's in.

An example of a global variable would be something like the Application object in ASP.Net (a public static instance of an HttpApplication class). Any object, at any time, in any code-behind file, can have access to the Application object to look for that value. Generally, storing values to the Application object is a bad idea unless you really know what you're doing, for all the reasons mentioned in the article that you linked.

womp
  • 115,835
  • 26
  • 236
  • 269
2

No. Global variables are variables which are available to your entire program. Private member variables are effectively the opposite of global variables. In C/C++ a global variable would be one which is declared outside of a function or class declaration. In C++ and Java, a public static variable which is not constant would also be considered to be a global variable, since the entire program can access it.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
0

In C# there really isn't anything that could be called a "global" variable. The closest would be a public static variable; something that is available to all parts of the program at any time.

Randolpho
  • 55,384
  • 17
  • 145
  • 179
0

Basically there are other mechanisms that can lead to global variables introduction:

  • AppDomain.GetData/AppDomain.SetData pair - Gets/Sets the value stored in the current application domain for the specified name which is accessible either from other application domains.
  • Thread.GetData/SetData pair - Gets/Sets the value from the specified slot on the current thread, within the current thread's current domain.

As the arcticle states it is better to avoid them.

Dzmitry Huba
  • 4,493
  • 20
  • 19
0

I thinks Varialbes defined in the top of class with "DIM" still class scope and not Global, but if they defined as "PUBLIC" on top of class or "PUBLIC" in "MODULE" they could be "GLOBAL" , the GOOD you can access these variables form the ALL solution, the BAD , they will stay in MEMORY..

MoniR
  • 1,351
  • 12
  • 18