Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project there is a Base form. In this form there is a document tab which my user controls will loaded in separate document windows in Base form. I want to have a global variable that all user controls see that. How can I do this
Posted

Formally, .NET does not have global fields in the same sense and in other languages (please see my comment to Solution 1). And this is good: you never really need them; using global variables compromises the quality of code. However, Solution 1 suggests a functionally equivalent to what you want.

But you don't really need even that. You need some singleton. If the form you mentioned is a main form, it is, in almost all cases, is already a singleton. So, you should better have a regular instance (non-static) members of this form. You can always access it through this form instance from other places of the code. Still, you can make it internal static, but this is not an elegant solution.

If you really need a singleton, you need to use the singleton pattern correctly: http://en.wikipedia.org/wiki/Singleton_pattern[^].

Now, there are a number of incorrect or just bad, unsupportable or unsafe implementation of the singleton. Here you will find good correct implementations: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

Event the correctly implemented pattern is questionable. It simply means that it should be used quite rarely and with care. Please read:
http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial[^],
http://www-106.ibm.com/developerworks/library/co-single.html[^].

—SA
 
Share this answer
 
.NET doesn't have global variables, but you can do something similar by creating a class with static variables. This class can be static, but that's not required. But the variables in it have to be static:
C#
public static class GlobalVariables
{
    // Add your global variables here (don't forget to make the variables static). For example:
    public static string GlobalString; // or 'internal' if you don't want that your variables can be accessed from another assembly
    public static int GlobalInt = 5;
}

And in another class, access them using this way:
C#
GlobalVariables.GlobalString = "put your text here";
int i = GlobalVariables.GlobalInt;
 
Share this answer
 
v4
Comments
mit62 15-Apr-14 13:38pm    
I will define this static class. then for example if there are two user control, If I change GlobalString in UC1 to "newvalue",does the value in US2 will be updated to "newvalue"?
Thomas Daniels 15-Apr-14 13:42pm    
It depends. If you have local variables in UC2, whereof the initial value is set to the global variable, then no, the value won't be updated. If you always write GlobalVariables.VariableName without creating a local variable, then yes, it will be updated.
mit62 15-Apr-14 14:06pm    
ok.thanks a lot
Thomas Daniels 15-Apr-14 14:08pm    
You're welcome!
Sergey Alexandrovich Kryukov 15-Apr-14 14:17pm    
It is not really "global". It is global from the standpoint of storage (in an AppDomain), but not directly globally accessible. Formally, it's very close. Using such thing is a bad idea.

Besides, it's wrong that it requires static class. This is just your mistake.

[EDIT]

Please see my answer.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900