473,785 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 27785
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.myGloba lField = x;

or

x = Globals.myGloba lField;

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 CurrentPrincipa l 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******@hotmai l.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.myGloba lField = x;

or

x = Globals.myGloba lField;

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
4371
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 doing it - is it not? Once the application has loaded the page it is cached and is immediately available for other pages. With global variables - the best thing to do would be to use application variables - so long as there weren't too many...
17
5631
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
33
3051
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 been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
7
3145
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 couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
41
10678
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 = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
2
5210
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 file, and what would I do to access it? (I am using VB.NET for my code) Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
1
29382
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
6
2566
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 variables improve data sharing between SQL statements. Version 9.5 introduces the concept of global variables, which are named memory variables that you can access and modify through SQL statements. Global variables enable you to share data between...
14
3675
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: I have a module (Public) and in it contains a few global variables (String data type). When I go to use these in a Form the value is "Nothing." Why is
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.