Winform Variable Area

Is it wrong to use most class-level variables in a form? Will they be considered global variables?

public partial class Form1 : Form { private string mode; private int x, y; public Form1() { InitializeComponent(); } } 

I use variables in several controls when I declare them at the class level.

+4
source share
6 answers

Those will be considered global class levels (to distinguish from global applications). The more important difference in this case is that they are private to the class.

Class-level globals use them, so I would definitely not call it bad practice. It is very useful to use global global clusters when you plan to expose them through property attributes . For instance:

  • public readonly properties whose values ​​are controlled by logic internal to your class.

  • public properties using both set and get accessors (including custom validation logic in setter.)

However, I would say that it is good practice to make things local, unless otherwise required. The reason is because you have a less volatile state that belongs to an instance of the class, so there are fewer possibilities for such errors:

 private int EvilMethod1() { x = (int) Math.Pow((double) y, 2); return x; } private int EvilMethod2() { y = (x + y) * 2; return y; } // Assignments depend on the current values of x and y, // as well as yielding unexpected side effects. private void PureEvil() { // Return value depends on current y; has side effect on x while assigning y. y = EvilMethod1(); // Return value depends on current x and y; has side effect on y while assigning x. x = EvilMethod2(); } 
+2
source

What I get from the question is that if you use as an individual form that is independent of any form, then all these variables will be private variables for the class. And if the form is called from another place. Then it will be private variables. If you really want to make a clear design, you can create public properties over private variables that you want to open for another class.

That way, you can put the restriction of access to another class in private variables by creating read-only properties so that other classes cannot change, but can access it.

+4
source

Those are not considered global variables. They are global only within the Form1 class, and not for the entire program.

+2
source

It depends on what the variables are used for.

If they are used in only one method, they must be local to this method.

If they describe the state of a class and are used in several places, they should be declared as members of the class.

+1
source

They are private to the class Form1

0
source

Not knowing what the intent of your form is, it's hard to say what you are doing, good or bad. The variables shown here have the scope class, and since they are private, they are not available outside of Form1 and are not considered global.

If you really want global variables, create a static class with private static variables and public static accessories / mutators (property in C #) and access the variable through a public property. See this answer for an example.

0
source

All Articles