How to make public variable in Visual C# ( Console and GUI)

Public Variables are made so you can access it in multiple construction or function, it will make our work easier and quiker.

So here is how to make public / global variable for both Console and GUI

in Console we simply write down public static (variable type) (variable name)
example :
         public static int score; // public variable
    static void Main(string[] args)
score : 0; //access it in main constructor
static void a(int score)
score = score +1; //or in other constructor

In GUI is easier, we simply write (variable)(variable name) outside all the constructor or function.

Example :
int a,b,c;
private void Form1_Load(object sender, EventArgs e)
 a = 1;
 b = 2;
 c = 3;
  private void btnEnter_click(object sender, EventArgs e)
  txtHasil.Text = a+b+c;

Leave a Reply