Winform变量作用域

发布于 2024-12-11 01:34:09 字数 253 浏览 0 评论 0原文

将大多数变量放在类级别的表单中是一种不好的做法吗?这些会被视为全局变量吗?

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

    public Form1()
    {
        InitializeComponent();
    }
}

当我在类级别声明变量时,我在多个控件中使用这些变量。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

恏ㄋ傷疤忘ㄋ疼 2024-12-18 01:34:09

我从问题中得到的是,如果您使用不依赖于任何形式的个人形式,那么所有这些变量都将是类的私有变量。如果从其他地方调用该表单。那么它也将是私有变量。如果您确实想要进行清晰的设计,那么您可以在要公开给其他类的私有变量上创建公共属性。

这样,您可以通过创建只读属性来限制对其他类的私有变量的访问,以便其他类不能修改但可以访问它。

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.

誰認得朕 2024-12-18 01:34:09

这些将被视为类级全局变量(以区别于应用程序全局变量)。在这种情况下,更重要的区别是它们对于类来说是私有的。

类级全局变量有其用途,所以我绝对不会称其为不好的做法。私有类全局变量的一个非常好的用途是当您计划通过 属性访问器。例如:

  • public readonly 属性,其值由类内部逻辑控制。

  • public 属性,具有 setget 访问器(在 setter 中启用自定义验证逻辑。)

但是,我想说,除非有其他必要,否则将事物本地化是一个很好的做法。原因是属于类实例的可变状态较少,因此出现此类错误的可能性较小:

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(); 
}

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(); 
}
只有一腔孤勇 2024-12-18 01:34:09

这些不被视为全局变量。它们仅在 Form1 类中是全局的,而不是整个程序。

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

滥情哥ㄟ 2024-12-18 01:34:09

这取决于变量的用途。

如果它们仅在单个方法中使用,则它们应该位于该方法的本地。

如果它们描述类的状态并在多个地方使用,则应将它们声明为类成员。

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.

不交电费瞎发啥光 2024-12-18 01:34:09

它们是 Form1 类私有的

They are private to the class Form1

夜灵血窟げ 2024-12-18 01:34:09

如果不知道你的形式的意图是什么,就很难说你所做的是好还是坏。此处显示的变量具有类范围,并且由于它们是私有的,因此无法在 Form1 外部访问它们,并且不被视为“全局”。

如果您确实需要全局变量,请创建一个包含私有静态变量和公共静态访问器/修改器(C# 中的属性)的静态类,并通过公共属性访问该变量。有关示例,请参阅此答案

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文