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 5 4918 da**@softwareonline.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.Current
Seem ok? da**@softwareonline.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.Current
Seems very standard to me.
Arne da**@softwareonline.com wrote:
da**@softwareonline.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.Current
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.
I use a UserPreferences class that contain a "static constructor",
static methods, static properties, etc. da**@softwareonline.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
Dave Herron wrote:
I use a UserPreferences class that contain a "static constructor",
static methods, static properties, etc.
da**@softwareonline.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
ConfigurationManager.AppSettings. That takes care of persistence as
well. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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,...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |