Global variables (ish) in C#

in Visual C#, how do you declare a variable so that is visible to any form in my project I used the following code to make an object called myRegistry that can be used to save settings in the correct place:

Microsoft.Win32.RegistryKey myRegistry = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("software").CreateSubKey("RockGem");

What would I have to do so that i declare this once and then am able to use it in any form i add to my project Is this even possible

Thanks,
-Javawag



Answer this question

Global variables (ish) in C#

  • PRSDeveloper

    Javawag,

    It might seem to work now, but it may not in the future. RegistryKeys are not designed to be used as static fields. I would really recommend that you go down the road of creating the RegistryKey each time, like I suggest above.



  • KyleLewis

    ok, so how would I declare that, and where

    Thanks,
    -Javawag


  • SanooD

    Javawag,

    That's right, but wrap the registry key usage in a using like I did above, so that Registry Key is closed.

    As per the second question, start up a thread and post in the Windows Forms forum.



  • bslim

    hi,

    you can do something like this

    read the value of your key from the registry

    string bestTime = (string)Application.CommonAppDataRegistry.GetValue(myKey);

    change the value of the key in the registry

    Application.CommonAppDataRegistry.SetValue(myKey, Value );

    or you can save your variables in xml, or you can use static fields something like that

    class Program

    {

    static void Main(string[] args)

    {

    GlobalVars.MyfirstVar = "Any string";

    Console.WriteLine(GlobalVars.MyfirstVar);

    }

    }

    class GlobalVars

    {

    private static string myVar;

    public static string MyfirstVar

    {

    get { return myVar; }

    set { myVar = value; }

    }

    }

    hope this helps



  • racka4279

    Ok so is this alright :

    Declaration:
    public static Microsoft.Win32.RegistryKey key()
    {
    return Microsoft.Win32.Registry.CurrentUser.CreateSubKey("software\RockGem");
    }

    Usage:
    Microsoft.Win32.RegistryKey key = global.key();
    key.SetValue("test","test.");

    One last thing, how do you go about closing the first form of an application without the whole app closing I want to close the first form and then open a second form without the application closing completely.

    Thanks,
    -Javawag


  • nguyen_van

    you may use static fileds


  • Niels A-J

    sorry, i didn't red your post to deeply.
    indeed it is not a good idea to use registry keys this way. first of all you should rethink this. what for do you need registry keys in of all your forms coyldn't you read them once you lunch your app and then update them while closing the app


  • jerv_it

    Thanks !

    In the end I added a new public static class called global and declared the registry key there which seems to work ok and also in the future I can put other methods and variables in this global class and access them more easily!

    Thanks so much,
    -Javawag


  • CMR12963

    You can make use of static fields, see the following post:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=84781&SiteID=1

    However, be aware that I wouldn't make a RegistryKey as a static variable, these objects should only be created on demand and closed when finished. Also static fields need to be thread-safe if you are running multiple threads (for example, by using the BackgroundWorker component)

    Instead I would use a method that simply returned a new copy of the Registry key.



    public static class RegistryKeys
    {
    public static RegistryKey CreateKey()
    {
    return Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\RockGem");
    }
    }

    You can then do this:



    using (RegistryKey key = RegistryKeys.CreateKey())
    {
    // Do something
    }

    Hope that helps

    David



  • Global variables (ish) in C#