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 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? 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 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 BB |
last post: by
|
2 posts
views
Thread by Bryan |
last post: by
|
4 posts
views
Thread by BobRoyAce |
last post: by
|
2 posts
views
Thread by Paul |
last post: by
|
1 post
views
Thread by Tom Berger |
last post: by
|
6 posts
views
Thread by Rajesh |
last post: by
| | | | | | | | | | |