c# - How to declare a variable for global use -


i've searched forum similar issues , understand, global variables avoided. me not logical yet, i'm new programming. if i've understood correctly, static variable should job i'm looking for.

i've made combobox of 4 choices in mainwindow , when comboboxitem selected, variable b declared. done in private void selectionchanged.

when comboboxitem declaring variable b selected, usercontrol pops up. want use variable b further in program, can't access it. i've tried declare static int b; in beginning of code, i'm not sure if understand use of static variable correctly. can please me?

avoid global variables , static keyword @ unless 100% sure there no other address solution (sometimes might forced use statics typically legacy code hot fixes).

  1. statics/globals make tight code coupling
  2. breaks ood principles (typically dependency injection, single responsibility principles)
  3. not straightforward type initialization process many think
  4. sometimes makes not possible cover code unit test or break atrip principles tests (isolated principle)

so suggestion:

  1. understand problem in first place, roots, going achieve
  2. review design

Comments