472,143 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

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 4853

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?

Jan 21 '07 #2
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
Jan 21 '07 #3

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.

Jan 21 '07 #4
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
Jan 21 '07 #5

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.

Jan 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Oystein Haare | last post: by
2 posts views Thread by Gianguz | last post: by
6 posts views Thread by MechSoft | last post: by
4 posts views Thread by BobRoyAce | last post: by
1 post views Thread by Tom Berger | last post: by
reply views Thread by leo001 | last post: by

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.