Best way to handle system wide values

  • Thread starter Thread starter Jeff Williams
  • Start date Start date
J

Jeff Williams

I know c# does not allow global variables so I am wondering the best way
to achieve this.

On start of my application I want to read values from the registry and
then different forms will need to access the data.

What is the best way to achieve this.
 
Jeff,

When needed I create a class and initilize in either the main form or
program as static and everything can see it. Just use care when access or
updating values from different threads.

public class clsGlobalData
{
public static clsGlobalData myData = new clsGlobalData();

}

from other code just do:

clsGlobalData.myData.<somthing>

Regards,
John
 
It allows static values, though, often found in static classes (but not
always)...

static class MyAppConfiguration {
public readonly string SomeSetting;
public readonly int SomeOtherSetting;

static MyAppConfiguration() {
// some code that sets SomeSetting and SomeOtherSetting,
// for example by looking at a config file or the registry
}
}

(or alternatively by some kind of static Load() method);
this will make these settings available to every where, every thread,
via the static accessors:

string something = MyAppConfiguration.SomeSetting;

Note that if you want to make them read/write, then you should either
satisfy yourself that you will only ever have one thread, or you should
read up on thread synchronisation (and use properties, not fields, to
achieve this; fields is just me being VERY lazy for a demo - I usually
never make fields public).

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top