473,739 Members | 7,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where to place "global" objects such as user preferences

If I have a class that hold, for instance, user settings that should be
accessible to the entire program logic, what is a good paradigm to use?

In C++, I would have made it a global object, protected if necessary
for thread safety. In C#, of course, there are no global objects.

The Program object is static, so cannot contain object instances.

While I could store it in my main form pass it around, that seems
cumbersome. I could do a singleton, but this seems like a lot of work
just to get around the "better programming style precludes global
objects" imperative.

What is the simple yet "proper" method people tend to use for this in
C# (to which I am new)?

Thanks!
Dave

Jan 21 '07 #1
5 4937

da**@softwareon line.com wrote:
If I have a class that hold, for instance, user settings that should be
accessible to the entire program logic, what is a good paradigm to use?

In C++, I would have made it a global object, protected if necessary
for thread safety. In C#, of course, there are no global objects.

The Program object is static, so cannot contain object instances.

While I could store it in my main form pass it around, that seems
cumbersome. I could do a singleton, but this seems like a lot of work
just to get around the "better programming style precludes global
objects" imperative.

What is the simple yet "proper" method people tend to use for this in
C# (to which I am new)?

Thanks!
Dave
To answer my own question (and to get things done while I wait for the
group wisdom), I wound up doing a singleton, with the instance called
"Current".

Thus, I reference the single UserSettings object from anywhere within
the program as

UserSettings.Cu rrent

Seem ok?

Jan 21 '07 #2
da**@softwareon line.com wrote:
To answer my own question (and to get things done while I wait for the
group wisdom), I wound up doing a singleton, with the instance called
"Current".

Thus, I reference the single UserSettings object from anywhere within
the program as

UserSettings.Cu rrent
Seems very standard to me.

Arne
Jan 21 '07 #3

da**@softwareon line.com wrote:
da**@softwareon line.com wrote:
If I have a class that hold, for instance, user settings that should be
accessible to the entire program logic, what is a good paradigm to use?

In C++, I would have made it a global object, protected if necessary
for thread safety. In C#, of course, there are no global objects.

The Program object is static, so cannot contain object instances.

While I could store it in my main form pass it around, that seems
cumbersome. I could do a singleton, but this seems like a lot of work
just to get around the "better programming style precludes global
objects" imperative.

What is the simple yet "proper" method people tend to use for this in
C# (to which I am new)?

Thanks!
Dave

To answer my own question (and to get things done while I wait for the
group wisdom), I wound up doing a singleton, with the instance called
"Current".

Thus, I reference the single UserSettings object from anywhere within
the program as

UserSettings.Cu rrent

Seem ok?
That's the nice solution.

In most of my programs I have very few settings, so I tend to make them
local variables in Main and then pass them to the classes where they're
needed, where they become statics. A UserSettings singleton is a
cleaner solution, though.

Jan 21 '07 #4
I use a UserPreferences class that contain a "static constructor",
static methods, static properties, etc.

da**@softwareon line.com wrote:
If I have a class that hold, for instance, user settings that should be
accessible to the entire program logic, what is a good paradigm to use?

In C++, I would have made it a global object, protected if necessary
for thread safety. In C#, of course, there are no global objects.

The Program object is static, so cannot contain object instances.

While I could store it in my main form pass it around, that seems
cumbersome. I could do a singleton, but this seems like a lot of work
just to get around the "better programming style precludes global
objects" imperative.

What is the simple yet "proper" method people tend to use for this in
C# (to which I am new)?

Thanks!
Dave
Jan 21 '07 #5

Dave Herron wrote:
I use a UserPreferences class that contain a "static constructor",
static methods, static properties, etc.

da**@softwareon line.com wrote:
If I have a class that hold, for instance, user settings that should be
accessible to the entire program logic, what is a good paradigm to use?

In C++, I would have made it a global object, protected if necessary
for thread safety. In C#, of course, there are no global objects.

The Program object is static, so cannot contain object instances.

While I could store it in my main form pass it around, that seems
cumbersome. I could do a singleton, but this seems like a lot of work
just to get around the "better programming style precludes global
objects" imperative.

What is the simple yet "proper" method people tend to use for this in
C# (to which I am new)?

Thanks!
Dave
If your using .net 2.0 you could consider looking at
ConfigurationMa nager.AppSettin gs. That takes care of persistence as
well.

Jan 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3036
by: Oystein Haare | last post by:
I'm thinking about sort of a factory-system where the factories get instanciated on program start (through a global object in the .cpp file), and registers themselves with a singelton. Is it a good idea to create global objects like that? Maybe it won't work at all? code: class AbstractFactory { ...};
2
1557
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables (objects and scalar-types). In the following example Global is able to create objects of any kind with and index value attached to. So a Global<0, string> is a unique string instance object allocated into the system that can be accessed from any...
6
1350
by: MechSoft | last post by:
Hi, I am new to C++ from C, I am a bit confused about using global objects in C++. I have a program that need to share some data(held in classes) between files, and of course I thought about using global objects. But then I notice lots of people said it is something to avoid doing. Can someone tell me where to draw the line to use them or not? Thanks in advance.
4
3536
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public myVar as string, etc. It seems to me that the scope and duration are the same, as they both are there while the application is running, and both go away when it quits. I presume that one difference is that the application state can be "flushed," such...
2
1271
by: Bryan | last post by:
I am currently working on a project that I am interested in porting to Win32 or .NET, but I don't know where to get started. I have quite a bit of experience with MFC, and I have been working with C# for the last few months and I feel like if I know where to start looking, I can do this. The current app is for Mac OS X and is being built with Cocoa and Objective-C. Bascially, it is a system service that allows users to paste things to...
4
1270
by: BobRoyAce | last post by:
I posted the post below in another newsgroup and it has been suggested that this may be a better place to post it. See below: ---ORIGINAL POST BEGINS--- I have an application which is comprised of, among many other things, a Main form which shows various user controls depending on which NavBar link they click on. In addition, there are many Entity classes that I created to support the application, one of which happens to be called User....
2
1554
by: Paul | last post by:
I am taking over an exisitng app. I have the following function on one page: function check($v) { global $user; return $user->validate($v, $v); } And it reference $user on the next page. Register_globals are set on the existing server but I am moving to a new server where it is not. The code
1
3385
by: Tom Berger | last post by:
Just a short question concerning exception handling.... All of my applications contain an event handling in the Main() like this one: static void Main() { // declare global exeption handler Application.ThreadException += new
6
1541
by: Rajesh | last post by:
I read Global Object's constructor will be called before main() function; In which situation it can be really helpful? Is it good practice use Global object and its constructor ? Thanks, Rajesh.S
0
8792
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
9479
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
9337
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...
1
9266
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
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
6754
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
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
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...
3
2193
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.