Uses a static property in a bad practice form, knowing that there is only one instance of the form?

In complex form, I have a property called Readonlythat determines whether everything is edited or not. Until now, I passed this property to each subordinate control in this form by the constructor, and in other places I turned to the form itself to get the value.

But it quickly gets too complicated.
I am going to make this Static property on the form, knowing that there is only one instance of this form in the application.

Can this property be used as static in this case? Or this is bad practice, even there is only one instance of the form.

+5
source share
3 answers

Just ask yourself: does this apply to the form or type of form. Hypothetically, if there were more than one form, would they all be read / not at the same time? Or will it be in shape?

Then: you have an answer. I suspect this must be an instance (non-static).

+3
source

Even if you have one instance of the form using a static field, this does not make it safe. You may have multiple threads that cause problems. Not to mention the complexity of the unit test of your application. Personally, I try to avoid static fields as much as possible.

+4
source

:

  • , .
  • Create an interface with a name IReadOnlyToggablethat has a property IsReadOnlyand let the form implement it.
  • Add the following properties to your custom elements:

the code:

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

All Articles