473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 27748
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
2
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
6
by: Frank Swarbrick | last post by:
Interesting! I was going to ask if such a thing existed, but I was pretty much convinced they did not so I didn't ask. Looks like with version 9.5 DB2 supports global variables: "Global...
14
by: Fred Block | last post by:
Hi All, I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and I'm very excited. Here's an issue I'm experiencing right off the starting line and cannot make sense of it:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.