469,579 Members | 1,063 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Global application variables in WinForms app

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
Nov 16 '05 #1
2 27419
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

Nov 16 '05 #2
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


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Michael | last post: by
41 posts views Thread by Miguel Dias Moura | last post: by
2 posts views Thread by Nathan Sokalski | last post: by
6 posts views Thread by Frank Swarbrick | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.