Global Variables

Using a Variable in All Subroutines

The programs we write in C# consist of more than one subprogram and methods. As it is known, every subroutine and method begins and ends with curly braces.

In which subprogram or method a variable is declared, it is valid between the parentheses. For example, in the picture below, a variable named number is defined in the button1_Click program. This variable cannot be used in the button2_Click program. Because it is not valid in that program, it is valid only in button1_Click program.

We can define and use a variable with the same name in the button2_Click program. Because different programs can define a variable with the same name, and these variables are independent from each other even though they have the same names. Setting a value in one does not change the value of the other.

If the place where the variable is defined is changed as follows, other subprograms and methods can also use this variable.

Here the number variable can be used by all subprograms. When the value of a variable is changed anywhere, its new value will apply to all of them, since all programs use the same variable.

c# global variable tutorials, what is public, use the variable in all subprograms, the life cycle of a variable, where the variable is valid

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 522 times.

Online Users: 519



using-global-variables