2

I've searched forum for similar issues and as I understand, global variables is to be avoided. For me that is not logical yet, as I'm new to programming. If I've understood all this correctly, a static variable should do the job that I'm looking for.

I've made a combobox of four choices in the mainwindow and when a comboboxitem is selected, variable b is declared. This is done in a private void SelectionChanged.

When the comboboxitem declaring variable b is selected, a usercontrol pops up. I want to use variable b further in my program, but I can't access it. I've tried to declare static int b; in the beginning of the code, but I'm not sure if I understand the use of a static variable correctly. Can someone please help me?

4 Answers4

4

Avoid global variables and static keyword at all unless you 100% sure there is no other address your solution (sometimes you might be forced to use statics typically with legacy code hot fixes).

  1. Statics/globals make tight code coupling
  2. Breaks OOD principles (Typically Dependency Injection, Single Responsibility principles)
  3. Not so straightforward type initialization process as many think
  4. Sometimes makes not possible to cover code by unit test or break ATRIP principles for good tests (Isolated principle)

So suggestion:

  1. Understand Problem in the first place, roots, what are you going to achieve
  2. Review your design
sll
  • 61,540
  • 22
  • 104
  • 156
  • I think maybe this is the answer to my problem, my design should be reviewed og revised. If I understand this correctly, it's not possible to pass a variable from one window to another, in this example a usercontrol, and use it further in the calculations without using a global variable. – Thomas Eikje Gjerde Jan 29 '16 at 12:20
  • @ThomasEikjeGjerde if you want to pass data between windows, then you can use `EventArgs` in `events`. – StepUp Jan 29 '16 at 12:39
3

It is possible to create a variable for global use. Just create static field or property:

public static class YourStorage
{
   public static object Storage1;
   public static string StringStorage;
} 

And wherever you want, you can just set or get values from that storage:

public class AnotherClass
{
   private void GetDataFromStorage()
   {
      string getValue=YourStorage.StringStorage; 
   }
   private void SetDataFromStorage()
   {
       YourStorage.StringStorage="new value"; 
   }  
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
3

you can do this insted

App.Current.Properties["valueTobestored"] = valueTobestored;

And later access it like

string mystoredValue = Convert.ToString(App.Current.Properties["valueTobestored"]); 
Vishnu Babu
  • 1,183
  • 1
  • 13
  • 37
1

To create a "global variable", it should be public and static, and declared in a public static class. In .NET it's a common way to declare constants (e.g. Math.PI), but they're not variables!

public static class EveryoneCanSeeMe
{
    public static object EveryOneCanModifyMe;
}

non-public variables are only visible in classes or methods where they're declared.

ps: using global variables is very bad.

Mike Tsayper
  • 1,686
  • 1
  • 17
  • 25
  • 1
    If you want constant, use `public const` – Arghya C Jan 29 '16 at 11:49
  • 2
    "ps: using global variables is very bad." is not true. Ignore this. Everything is relative. Sometimes it's bad to use, sometimes it makes sense. – Yusha Dec 12 '17 at 15:12