views:

1362

answers:

7

What's the best practise for storing global variables in a VB.NET WinForms app. For example when a user logs into an app you may want to store a CurrentUser object which can be accessed throughout the app. You could store this as an object in a module or create a class which contains members for all the required globals, you would still need to store the instance of this somewhere though.

Does the framework provide an easy solution to this?

+3  A: 

You can store globally accessible variables as public readonly static properties of the appropriate class, such as the one that creates or populates it.

Otávio Décio
+7  A: 

There is approximately one best practice regarding the use of global variables.

"Don't."

(If this sounds harsh, consider that things like CurrentUser normally belong in something that the environment already maintains a unique instance of for you, such as the Session. Look up the API to obtain the current session, store your CurrentUser there and retrieve it from there. Don't create your own globals, which will render your app harder to maintain and vulnerable to race conditions.)

Morendil
Where would I find the Session in a WinForms application?
Colin Mackay
A: 

The singleton pattern is safer than a garden variety global.

Andrew Cowenhoven
Singletons might be "glorified globals". But this does not make them a good approach to global variables. [See all the nice problems with Singleton instatiation regarding Threading.]
Leonidas
+2  A: 

Using static/global variables is severely detrimental to the testability of your code, since a test cannot be sure that the objects it manipulates are not causing side-effects in seemingly-unrelated areas of the codebase.

If you have a class that needs an instance of the CurrentUser class, make it ask for it in its constructor.

See here for more info: http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/

Krougan
One more link that discusses this topic: "Global State and Singletons" http://www.youtube.com/watch?v=-FRm3VPhseI
Esko Luontola
+11  A: 

I think 'don't' is a little harsh, here's a quote from Steve McConnell:

Used with discipline, global variables are useful in several situations

I think just like a good carpenter has the right tool for the job and will use the right tool if the need arises, programmers should also use all of the tools at their disposal.

Straight from the 'Tour de Force' Code Complete are several reasons to use global data:

  • Preservation of global values
  • Streamlining use of extremely common data
  • Eliminating tramp data

McConnell also says:

Use Global Data Only as a Last Resort. Before you resort to using global data, consider a few alternatives.

here are the alternatives he lists:

  • Begin by making each variable local and make variables global only as you need to
  • Distinguish between global and class variables
  • Use access routines

The things i've mentioned here get great coverage in the fantastic book Code Complete

Phaedrus
**Code Complete** should be required reading.
AMissico
"Use access routines" <<--- The most important
Burnsys
A: 

In System.Threading.Thread.CurrentPrincipal.Identity.Name .... But couldn't remember how it was done ... Just do a google will help you ....

A: 

My practice is to put global variables like CurrentUser, ConfigFilePath and similar into program.vb (startup class with sub main). That way nobody uses global variables mindlessly as their must be accessed through program namespace.

In business logic, I never use global variables directly. If any function needs to use global variable, it must be sent as a function parameter.

There is no way to avoid using global variables. IMHO, it's better to call them with their real name and use them carefully then to mask them in singletons, sessions or files.

Hugo Riley