I would just add that there is usually a "better" way. For instance, in
the case of the user information you mentioned, you can implement
IPrincipal and attach your custom principal to the application thread
(see the CurrentPrincipal property of Thread). The user principal is
then available to the entire application, and it's in a place that makes
sense to other .Net programmers.
The thing to avoid is creating a single global object that holds a bunch
of unrelated data. Future maintenance programmers will appreciate your
efforts. :)
One other thing - when you start designing classes like this, the
singleton pattern can be useful sometimes. Here's a great article on
implementing them:
http://www.yoda.arachsys.com/csharp/singleton.html
Regards,
-Jeff
pd******@hotmail.com wrote:
Use a class with static members. Occasionally, I will create a class called
Global where all the members are static so that I can access the members
from any class.
class Globals
{
public static int myGlobalField;
}
then from anywhere in the app, simply do:
Globals.myGlobalField = x;
or
x = Globals.myGlobalField;
simple.
Pete
"David McCormack" <no****@zamang.co.uk> wrote in message
news:10****************@damia.uk.clara.net...
I have a small project that I'm writing to help me learn C# and the .Net
Framework. This project is a WinForms program with multiple forms that
accesses MSDE. I've got most of it done but I've hit a slight brick wall.
Does anyone have any recommended strategies for dealing with application
wide variables? i.e. the user name of the current user that's logged in
to the application rather than the PC.
The best idea I've come up with so far is to store the items in the
Windows registry, so they can be read as and when required, but its not
ideal. Every other idea I've come-up with requires objects to be passed
to single form when its opened, which I would rather not do as it'll
mean override routines in every single form.
Looking on Google I could only find items relating to WebForms, and the
use of Session variables.
Many thanks in advance,
David McCormack