Link to home
Start Free TrialLog in
Avatar of bussd
bussd

asked on

How to create a global settings object using singleton and hashtable

Hello

I'm trying to create an object called Settings that will store a set of values to be used throughout my application as well as read and write them in the registry.  So far I've defined a singleton called clsSettings with the following attributes:

Properties:
public string RegistryPath //set this property to determine where the values are stored in the registry
public Hashtable Values //key-value pair where the key is the name of the setting (eg. "InputDir") and value is the value of that setting (eg. "C:\test")

Methods:
public static GetInstance //Returns the persistent instance of my singleton clsSettings object
public bool Read (string default) //iterates through Values and looking for each Key in RegistryPath and assigning it to Value (not yet implemented)
public bool Write //iterates through Values and writes each Key to RegistryPath creating registry keys as necessary (not yet implemented)

I have a few questions:

1) Is this the best design for what I'm trying to do?
2) What's the easiest way to pull a value out of a hashtable?
3) Ideally, I would like to access my values like this:  Settings.Values["InputDir"] = "c:\test".  Is there an existing way for me to do this or would I have to build my own?
4) Do I have to explicitly destroy my singleton?

As you can tell I'm new to C# and appreciate any help you can offer.  :)

thanks!
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bussd
bussd

ASKER

Thanks for the quick response!

Is there a way to store data of different types in my hashtable, or am I limited to strings?

Also, being new to C#/.NET I'm not sure what you mean by unmanaged resources.  Please explain?
The hashtable stores everything as an Object, ou can store any type, but you'll have to cast it back when you retrieve it:

int intValue = (int)hashtable["key1"];
string stringValue = (string)hashtable["key2"];

etc.  As long as you know what type it is, you can directly cast it.  If the types aren't compatible, you will get an InvalidCastException.

unmanaged resources are things like File Handles, COM object pointers, etc.  If you're just using basic types (int, string, bool), or .NET classes, you con't need to explicitly dispose of the singleton.
Avatar of bussd

ASKER

It seems like I can't assign anything to Values ("NullReferenceexception was unhandled").  Do I need a set accessor for my _Values property?  If so, what would that look like?
For a singleton class you should assign in in the private constructor:

private Settings()
{
  this._Values = new Hashtable();
  this._RegistryPath = {set value};
}

You generally don't have set accessors for singleton properties because you run into issues of what is responsible for setting the value.  Singletons should generally have the same values across all instances (that's why it's a singleton).
Avatar of bussd

ASKER

I can now assign values (thanks!), but I'm confused about this:

> You generally don't have set accessors for singleton properties because you run into issues of what is responsible for setting the value.

Doesn't using the GetInstance() method prevent multiple instances?  If so then how could you have different values for the same property/variable?

I would like to set RegistryPath from outside the class (eg. clsSettings.GetInstance().RegistryPath = "whatever") and to do that I would need a set accessor on RegistryPath (eg. public string RegistryPath { get { return _RegPath; } set { _RegPath = value; } })

 ... is that right?
You still have one instance, but the point of using singletons is to have one instance, so several "clients" can share the data (or functions, whatever).  Generally (not always) the _value_ of that data is set when the singleton is first created (hence set in the constructor).  Otherwise, you always have to check to see if the value has been set, and if not, either set it or do without.  So the question is, who decides what values the singleton will have?  Can just anyone change the values?  That's why you typically have only get accessors, so you can control who can set the values.

Like I said, these are _general_ rules, and your application may have a different implementation, but that's how I've typically seen singletons implemented.
Avatar of bussd

ASKER

Got it.  Thanks again for all your help.