views:

233

answers:

2

Hi,

How would I achieve something like this in C#?

object Registry;
Registry = MyProj.Registry.Instance;

int Value;
Value = 15;
Registry.Value = Value; /* Sets it to 15 */
Value = 25;
Value = Registry.Value; /* Returns the 15 */

So far I have this object:

namespace MyProj
{
    internal sealed class Registry
    {
        static readonly Registry instance = new Registry();

        static Registry()
        {
        }

        Registry()
        {
        }

        public static Registry Instance
        {
            get
            {
                return instance;
            }
        }
    }
}
+2  A: 

You should be using the Microsoft.Win32.Registry class. It, and the related classes, have all you need to work with the Windows registry.

tvanfosson
+4  A: 

Simply add a property to your Registry class:

internal sealed class Registry
{
    public int Value { get; set; }
    ...
}

Then use like this:

Registry theRegistry = MyProj.Registry.Instance;
//note: do not use object as in your question

int value = 15;
theRegistry.Value = value; /* Sets it to 15 */
value = 25;
value = theRegistry.Value; /* Returns the 15 */
M4N
... and make it 'static' ;-)
Thomas Weller
@Thomas: make what static?Registry is a singleton. The Value property is an instance property accessed via the static Registry.Instance property.
M4N