How can I share my custom user settings (ApplicationSettingsBase) with several objects?

In my program I have several classes:

WebServer
EmailClient
TelnetClient

...and they all use configurable data which I have in a class called "GNotifyUserSettings" (which is a descendant of ApplicationSettingsBase).

Do I need to create a GNotifyUserSettings object in every class I want to use custom settings in ...or should I pass in an object reference to the user settings object to every class

Neither method seems very clean. What I'd really like is a "global object" that every class can access... (and it must be thread-safe)



Answer this question

How can I share my custom user settings (ApplicationSettingsBase) with several objects?

  • Aleniko29139

    I'm using VS2005, but I'm trying to do everything programatically...
  • Andre's

    Sorry about taking a bit to get back to you on this...

    When I suggested making your class instance static I was not referring to the class definition.

    Somewhere in your code you’ve got maybe...

          GNotifyUserSettings mySettings = new GNotifyUserSettings();

    And then your issue was how to either make this globally available or pass it around... the easiest way to do so is to change the declaration to:

          public static GNotifyUserSettings mySettings = new GNotifyUserSettings();

    Which will enable you access the mySettings instance from just about anywhere within the app.

    Peter also hints at one other possibility if you are using the 2.0 Framework... to use the new Settings system that it supports.



  • MechiF

    sofakng wrote:
    Do I need to create a GNotifyUserSettings object in every class I want to use custom settings in ...or should I pass in an object reference to the user settings object to every class
    Are you using VS2005 or VS2003

  • Sp1ice69

    In order to make your instance of GNotifyUserSettings a global object you could simply declare your instance with both the 'public' and 'static' keywords out front... this way you do not need to create an instance of the type it is contained within.

    Would this work for you



  • Neotech

    sofakng wrote:
    I'm using VS2005, but I'm trying to do everything programatically...
    I would suggest using VS2005's built-in support for configuration settings, both user and application.

    It will allow you define user settings, given default values and access them globally through the Settings class.



  • David Maynard

    I'm a bit confused...

    I've changed this:

    public class GNotifyUserSettings : ApplicationSettingsBase

    ...to this:

    public static class GNotifyUserSettings : ApplicationSettingsBase

    And now I get a ton of errors:

    settings.cs(7,45): error CS0713: Static class 'GNotifyAddon.GNotifyUserSettings' cannot derive from type 'System.Configuration.ApplicationSettingsBase'. Static classes must derive from object.settings.cs(11,18): error CS0708:

    'GNotifyAddon.GNotifyUserSettings.CommandTimeout': cannot declare instance members in a static class
    'GNotifyAddon.GNotifyUserSettings.GliderServerAddress': cannot declare instance members in a static class
    (...etc)


  • How can I share my custom user settings (ApplicationSettingsBase) with several objects?