11

This seems basic but Im finding this quite trivial. Simply how would you recommend setting a global variable with a static class (i.e. console-application)?

To give you a little more background the main method is calling some custom eventhandlers that I hope to get / set the variables.

Any ideas or suggestions you have is appreciated.

Leroy Jenkins
  • 2,680
  • 6
  • 25
  • 33

3 Answers3

14

Simplest way is

public static Object MyGlobalVariable;

which creates a public static field. A little better is:

public static Object MyGlobalVariable { get; set; }

Which creates a public static property.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
13

There are no global variables in C#. A variable is always locally-scoped. The fundamental unit of code is the class, and within a class you have fields, methods, and properties.

You can mimic a "global variable" by making a public static field or property in some class, but you shouldn't. C# makes this difficult for a very good reason; global variables are pure evil. They violate several good principles of OO design - encapsulation, loose coupling, and high cohesion, to name just a few.

I realize this is a beginner question, but I think it's because this is a beginner question that it's so important to be saying this. Now is the best time to start learning what tactics are actively discouraged or even dangerous in C#, and using a static field/property as a global variable is about six of them. There are legitimate uses for these constructs, but passing data around from place to place is not one of them.

If two different classes depend upon the same information, then pass the information from the source to the destination. This is usually done either through the constructor or as an argument to the method being called. You should always have one and only one instance that truly "owns" this information; making information "global" means that you can't reason about who or what might be depending on it at any given point in time.

Please consider this, and try to think about other ways you could share the information that you want to store in a global variable (i.e. by providing it as an argument to a constructor or method). If you're not sure, post an example of what you're trying to do and we'll help out.

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
  • Thank you for elaborating.. that was very helpful. – Leroy Jenkins Feb 17 '10 at 04:29
  • Supposing I have a couple of threads running simultaneously and away from the main thread and now when there is a failure in either of those threads, I want to increase the failure count. How would this be done then? – user20358 Mar 02 '15 at 08:54
  • @user20358: That's not really enough information to recommend a solution. What are the threads doing? Why would they fail? What are the consequences of failure? What precisely is the point of *counting* them, as opposed to logging them or taking some follow-up action? You should clarify your problem and ask a new question. – Aaronaught Mar 03 '15 at 04:31
  • ^ In my case, I am incrementing the counter with each thread that is created, and then decrementing when each one finishes, and waiting for all to finish before executing another command. – Jacob Alley Oct 24 '16 at 13:16
1

Not 100% sure but you could try a singleton to hold your variables. Without knowing what you are trying to accomplish it's hard to recommend if this solution wouldn't bite you down the road.

http://www.yoda.arachsys.com/csharp/singleton.html

Tone
  • 322
  • 1
  • 3
  • 16