4

Is it bad practice to put most variables at class level in a Form? Would these be considered global variables?

public partial class Form1 : Form
{
    private string mode;
    private int x, y;

    public Form1()
    {
        InitializeComponent();
    }
}

I'm using the variables in multiple controls when I declare them at class level.

Jonas
  • 589
  • 2
  • 4
  • 12

6 Answers6

4

What i get from the question is that if you are using as a Individual form that is not dependent on any form then all this variables will be private variables to the class. And if the form is called from somewhere else. Then also it will be private variables. If you really want to make a clear design then you can Create public properties over private variables that you want to expose to other class.

In that way you can put a limit access to the other class to the private variables by creating read only properties so that other classes cannot modify but can access it.

Nivid Dholakia
  • 5,272
  • 4
  • 30
  • 55
2

Those would be considered class-level globals (to distinguish from application globals.) The more important distinction in this case is that they are private to the class.

Class-level globals have their uses, so I definitely wouldn't call it a bad practice. A very good use for private class globals is when you plan to expose them through property accessors. For example:

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

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

However, I would say it's a good practice to make things local unless otherwise necessary. The reason is that you have less mutable state belonging to a class instance, so there is less potential for bugs like this:

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(); 
}
Rob
  • 5,223
  • 5
  • 41
  • 62
2

Those aren't considered global variables. They are global only within the Form1 class, not the entire program.

user1007672
  • 121
  • 3
1

It depends what the variables are used for.

If they are only used within a single method they should be local to that method.

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

Andrew Kennan
  • 13,947
  • 3
  • 24
  • 33
0

They are private to the class Form1

Joe
  • 80,724
  • 18
  • 127
  • 145
0

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

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

Community
  • 1
  • 1
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194