Winform变量作用域
将大多数变量放在类级别的表单中是一种不好的做法吗?这些会被视为全局变量吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我从问题中得到的是,如果您使用不依赖于任何形式的个人形式,那么所有这些变量都将是类的私有变量。如果从其他地方调用该表单。那么它也将是私有变量。如果您确实想要进行清晰的设计,那么您可以在要公开给其他类的私有变量上创建公共属性。
这样,您可以通过创建只读属性来限制对其他类的私有变量的访问,以便其他类不能修改但可以访问它。
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.
这些将被视为类级全局变量(以区别于应用程序全局变量)。在这种情况下,更重要的区别是它们对于类来说是私有的。
类级全局变量有其用途,所以我绝对不会称其为不好的做法。私有类全局变量的一个非常好的用途是当您计划通过 属性访问器。例如:
public readonly
属性,其值由类内部逻辑控制。public
属性,具有set
和get
访问器(在 setter 中启用自定义验证逻辑。)但是,我想说,除非有其他必要,否则将事物本地化是一个很好的做法。原因是属于类实例的可变状态较少,因此出现此类错误的可能性较小:
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 bothset
andget
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:
这些不被视为全局变量。它们仅在 Form1 类中是全局的,而不是整个程序。
Those aren't considered global variables. They are global only within the Form1 class, not the entire program.
这取决于变量的用途。
如果它们仅在单个方法中使用,则它们应该位于该方法的本地。
如果它们描述类的状态并在多个地方使用,则应将它们声明为类成员。
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.
它们是
Form1
类私有的They are private to the class
Form1
如果不知道你的形式的意图是什么,就很难说你所做的是好还是坏。此处显示的变量具有类范围,并且由于它们是私有的,因此无法在
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.