简体   繁体   English

知道表单只有一个实例,在表单不好的情况下使用静态属性吗?

[英]Is using a static property in a form bad practice knowing that there's only one instance of the form?

In a complex form, I have a property called Readonly that determines if everything is editable or not. 在一个复杂的形式中,我有一个名为Readonly的属性,它确定是否所有内容都是可编辑的。 So far, I'm passing this property to every sub custom control in this form by constructor, and in other places, I access the form itself to get the value. 到目前为止,我通过构造函数将此属性传递给此表单中的每个子自定义控件,在其他地方,我访问表单本身以获取值。

But this is quickly becoming too complex. 但这很快变得太复杂了。
I'm thinking of making this property Static in the form knowing that there's only one instance of this form in the application. 我正在考虑将这个属性设置为静态 ,知道应用程序中只有这个表单的一个实例。

Is it OK to use this property as a static in this case? 在这种情况下,可以将此属性用作静态吗? Or it's a bad practice even there's only one instance of the form. 或者这是一个不好的做法,即使只有一个表格实例。

Even if your have a single instance of the form using a static field doesn't make it safe. 即使您使用静态字段拥有单个表单实例也不会使其安全。 You could have multiple threads that cause problems. 您可能有多个导致问题的线程。 Not to mention the difficulty to unit test your application. 更不用说对您的应用程序进行单元测试的难度。 Personally I try to avoid static fields as much as possible. 我个人尽量避免使用静态字段。

Simply ask yourself: does this relate to the form or to the type of form. 简单地问自己:这确实涉及到窗体或窗体的类型 Hypothetically, if there were more than one form - would they all be readonly/not at the same time? 假设,如果有多个表格 - 它们是否只是同时读取/不同时? Or would it be per form? 或者是每个表格?

Then: you have the answer. 然后:你有答案。 I suspect it should be instance (non-static). 我怀疑它应该是实例(非静态)。

Here is an alternative solution: 这是一个替代解决方案:

  1. Add the controls to your form as usual 像往常一样将控件添加到表单中
  2. Create an interface called IReadOnlyToggable which has a IsReadOnly property and let the form implement it. 创建一个名为IReadOnlyToggable的接口,它具有IsReadOnly属性并让表单实现它。
  3. Add the following property to your custom controls: 将以下属性添加到自定义控件:

code: 码:

public bool IsFormReadOnly
{
    get 
    {
        var form  = ParentForm as IReadOnlyToggable;
        return form != null && form.IsReadOnly;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为C#表单提供指向同一表单的唯一实例的静态属性是一个坏主意吗? - Is it a bad idea to give a C# form a static property pointing to the only instance of the same form? 使用FormCollection或Request在控制器中捕获表单数据是一种不好的做法 - Is it bad practice to catch form's data in controller with FormCollection or Request 是否有任何属性可用于了解后台运行的表单 - is there any property for knowing which form is running in background 从UserControl实例化表单是不好的做法吗? - Is it bad practice to instantiate a Form from a UserControl? 将新的SqlConnection实例包装在静态方法中是不好的做法吗? - Is it bad practice to wrap a new SqlConnection instance in a static method? 仅创建采用该类型实例的静态方法是不明智的做法 - Is it bad practice to only create static methods that take instances of that type 如何从另一个表单访问一个表单的属性? - How to access one form's property from another? 应用程序仅在Windows中运行一个实例并在隐藏主窗体时显示窗体 - app to run only one instance in Windows and show form when main form is hide 创建一个表单的多个实例? - Create multiple instance of one form? 将几种形式简化为一种形式。 好的还是坏的做法? - Reducing a few forms into 1 form. Good or bad practice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM