473,690 Members | 3,019 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 4933

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
3032
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
1555
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
1348
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
3533
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
1269
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
1266
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
1552
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
3382
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
1540
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
8598
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
9077
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
8951
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
7623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6457
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
5811
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
4322
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...
1
2965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1952
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.