Where to place "global" objects such as user preferences

D

dave

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
 
D

dave

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?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 
B

Bruce Wood

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.
 
D

Dave Herron

I use a UserPreferences class that contain a "static constructor",
static methods, static properties, etc.
 
A

Antony Denyer

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

If your using .net 2.0 you could consider looking at
ConfigurationManager.AppSettings. That takes care of persistence as
well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top