views:

34

answers:

1

Hi all,

This may seem like a dumb topic, but I'm trying to learn some good coding practices.

I'm making a windows.forms application and I have reached a point where my partial Form class has 7 global variables (and their corresponding properties) declared and used - to name a few: one to determine if the app is registered, a Settings object that I need to access in many events, a Logger object, etc.

Is this bad coding? I mean, is it ok to declare a bunch of global variables and make use of them in the various event handlers/methods, or is there some better practice and I should rethink my code, to avoid having them?

I have a lot of work left to do in this application so it's in a first phase now, and with my coding style it looks like it will end up having even more than 20 global variables.

Thank you in advance.

Best regards,

/Andrei

+1  A: 

You might consider adding an App.config file and using this for your global settings (such as logger setup, etc). An advantage of this approach is that you can change these settings and restart the app without making code changes.

You can add key/value pairs in your App.config:

<appSettings>
  <add key="YourKey" value="YourValue"/>
</appSettings>

and access them in code:

string yourValue = ConfigurationManager.AppSettings["YourKey"];

Performing the proper parsing (e.g. Int32.TryParse(..)) you can store data besides strings there.

Alex
Thanks, that will get me rid of some readonly int variables.
Andrei