3

How do I declare global variables in Visual C#?

neuromancer
  • 53,769
  • 78
  • 166
  • 223
  • You creating ASP.NET web page or Windows Form Application? If ASP.NET - see http://stackoverflow.com/questions/1797332/creating-global-variables-in-asp-net-using-c . If Form App - see http://stackoverflow.com/questions/1293926/c-winforms-global-variables – Michał Ziober Nov 27 '09 at 01:40

4 Answers4

15

How about this

public static class Globals {
    public static int GlobalInt { get; set; }
}

Just be aware this isn't thread safe. Access like Globals.GlobalInt

This is probably another discussion, but in general globals aren't really needed in traditional OO development. I would take a step back and look at why you think you need a global variable. There might be a better design.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Bob
  • 97,670
  • 29
  • 122
  • 130
  • In what sense is it not thread safe? And how is that any different from a non-static property? – Pavel Minaev Nov 27 '09 at 01:54
  • -1 for not explaining why it isn't thread safe... needs more explanation –  Nov 27 '09 at 01:56
  • 7
    This isn't a question about thread safety. If you are interested in thread safety then ask in another question or update this question to explicitly include a thread safe solution. Also see this question for what thread safety is if you never heard the term http://stackoverflow.com/questions/261683/what-is-meant-by-thread-safe-code – Bob Nov 27 '09 at 02:02
  • 2
    +1 for "I would take a step back and look at why you think you need a global variable. There might be a better design." I found in large projects, it is a lot more difficult to track the use of global variables opposed to instance variables. This helps, especially during maintenance. – Russell Nov 27 '09 at 02:26
3

A public static field is probably the closest you will get to a global variable

public static class Globals
{
  public static int MyGlobalVar = 42;
}

However, you should try to avoid using global variables as much as possible as it will complicate your program and make things like automated testing harder to achieve.

JohannesH
  • 6,430
  • 5
  • 37
  • 71
2

Use the const keyword:

public const int MAXIMUM_CACHE_SIZE = 100;

Put it in a static class eg

public class Globals
{
    public const int MAXIMUM_CACHE_SIZE = 100;
}

And you have a global variable class :)

Bob
  • 97,670
  • 29
  • 122
  • 130
Russell
  • 17,481
  • 23
  • 81
  • 125
  • Doesn't the const keyword make it constant? Can it be changed? – neuromancer Nov 27 '09 at 01:31
  • "The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified." - http://msdn.microsoft.com/en-us/library/e6w8fe1b%28VS.71%29.aspx – Michał Ziober Nov 27 '09 at 01:34
  • A const field isn't varable. ;) – JohannesH Nov 27 '09 at 01:36
  • lol yeah - i read the question too quickly, I usually only provide contants when exposing values globally. Everything else are in instance variables. :) – Russell Nov 27 '09 at 02:27
1

The nearest you can do this in C# is to declare a public variable in a public static class. But even then, you have to ensure the namespace is imported, and you specify the class name when using it.

Mark Bertenshaw
  • 5,594
  • 2
  • 27
  • 40