Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

static

In a complex form, I have a property called Readonly that determines if everything is editable or not. 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.

like image 954
French Boy Avatar asked Sep 03 '11 08:09

French Boy


2 Answers

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.

like image 160
Darin Dimitrov Avatar answered Oct 29 '22 18:10

Darin Dimitrov


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).

like image 24
Marc Gravell Avatar answered Oct 29 '22 17:10

Marc Gravell