469,343 Members | 5,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

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 4753

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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.