4

what is the best way to have data accessible throughtout the wole application? In my concrete example I load the settings of my application from an XML file into an instance of a Settings-Object, and I don't want to make these some absolute constants because the user should be able to change these (and see the effects) without restarting the program.

Now, I need to use certain of the (properties of the) settings in methods of other classes, but in this way they are not accessible. So in what kind of an 'Object' should I store the settings? I don't think it is good it each method that needs a setting across my application has to look into the XML itself. Also, passing the settings instance into every other class I use seems too cumbersome.

Thanks in advance!

Brian Rasmussen
  • 109,816
  • 33
  • 208
  • 305
EluciusFTW
  • 2,258
  • 6
  • 38
  • 54

4 Answers4

9

In C# I always use a static classes to provide this functionality. Static classes are covered in detail here, but briefly they contain only static members and are not instantiated -- essentially they are global functions and variables accessed via their class name (and namespace.)

Here is a simple example:

public static class Globals
{
    public static string Name { get; set; }
    public static int aNumber {get; set; }
    public static List<string> onlineMembers = new List<string>();

     static Globals()
     {
        Name = "starting name";
        aNumber = 5;
     }
}

Note, I'm also using a static initializer which is guaranteed to run at some point before any members or functions are used / called.

Elsewhere in your program you can simply say:

Console.WriteLine(Globals.Name);
Globals.onlineMembers.Add("Hogan");

To re-state in response to comment, static objects are only "created" once. Thus everywhere your application uses the object will be from the same location. They are by definition global. To use this object in multiple places simply reference the object name and the element you want to access.

Paedow
  • 3,402
  • 8
  • 35
  • 67
Hogan
  • 63,843
  • 10
  • 75
  • 106
  • Thanks Hogan, I tried this quickly and it seems to do what I want! Just for completeness, how do I do this, if I need several instances of the Obejct I want to use application-wide? – EluciusFTW Aug 02 '12 at 18:18
  • @ToaoG - As my edit says above, static objects are application-wide. – Hogan Aug 02 '12 at 18:49
  • Hogan, I think you misunderstood my last Q: I wanted (more generally) to know how to store/format data (like, e.g., runtime-generated object instances of a non-static object) so that it is accessible application-wide. Like the settings static object before, but for data where only at run-time I know how many (internally different) instanes I need. My guess is you put them in a collection and pass this collection around via references? – EluciusFTW Aug 02 '12 at 19:11
  • I'm not quite clear what you are asking since I think I'm covering it, but I added a collection to my example. – Hogan Aug 02 '12 at 19:21
  • Ok thanks yet again... I will have to think a bit more deeply as to what I mean exactly. Feel like having a knot in my head right now... – EluciusFTW Aug 02 '12 at 21:25
3

Define some simple Configuration (say) class:

public static class Configuration 
{
     /*runtime properties */

    public static void LoadConfiguration(..)
    {
       /*Load from file a configuration into the static properties of the class*/
    }


    public static bool SaveConfiguration(...)
    {
       /*Save static properties of the class into the configuration file*/
    }
}

Do not forget naturally default configuration, in case when config file for some reason missed.

Hope this helps.

Paedow
  • 3,402
  • 8
  • 35
  • 67
Tigran
  • 59,345
  • 8
  • 77
  • 117
0

Sounds like a perfect use of the Settings project page. You setup defaults, they can be modified and saved between runs of your application.

Andy
  • 8,054
  • 5
  • 35
  • 71
0

You can have a static class with static properties to get and set

swiftgp
  • 949
  • 1
  • 8
  • 15