Global Variable in Windows Application

I have created an MDI based application using C#. How and where can I create a global variable that can be used anywhere in the application



Answer this question

Global Variable in Windows Application

  • Jim D.

    Could you explain a little more please. That means if I want to run some process that does not need the MDI to be loaded, will not be able to access this global variable. Am I correct Also, Can't I create nonstatic global variables in the program.cs file


  • Geoff Dean

    Static varibles are not readonly. Check out the docs on static keyword in C# -- it means that there is only one instance of the variable.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    Program Manager

    Microsoft

    this post is provided "as-is"

  • Fer Mayorga

    OK, now I understand.

    Contents of program.cs
    ==========================================
    namespace Microsoft.Samples.Windows.Forms.DataGridViewSample
    {
       
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            string xxx = "YYY";
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                //Application.Run(new frm_Materials());
                Application.Run(new MDIParent1());
            }

        }
    }


    How should I define variable xxx so that I can access it anywhere in the application I tried DataGridViewSample..... but intellisense does not list the variable though.



  • BillRP

    No, the program.cs file is a static class called Program so all the members are required to be static as well.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    Is there a reason why you cannot use a static variable You can also create a static class property that returns an instance of a class where the class provides a non static property. This is called singleton class since every time you access this static property you'll get the same class.

     

    thanks,

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     

     


  • JL3

    Suppose I want to create a database connection programatically. How can I make this connection available throughout my application

  • Erik11

    Do this -- < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    1) double click the Properties node in the solution explorer and in the resources tab you'll see a grid.

    2) In Visual Studio 2005 you can add project-level strings and images via this tab. 3) In the "name" column, enter connectionString and

    4) in the "value" column, enter the connection information that you need to connect to your database. If you need more info on how to connect to your database, please ask a question on the .NET Framework Data Access and Storage forum (http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=45&SiteID=1 )

    5) Click the save all

    6) Next you can access this string anywhere in your program by the following code:


    Properties.Resources.connectionString
     

     

    Hope this helps!

    -mark

    Program Manager
    Microsoft
    This post is provided "as-is"

     


  • ChrisD

    Presumably, the static variable is static. However, I want to change the value of these variables run-time. In other words, say I created a static variable say my_var and assign value as "abc".

    At run-time I want to change the value abc to pqr and I need the value pqr be available until I restart the application.



  • Kevin Draper

    Static variables are global. Alternatively you can add a property to your MDIParent class to retrieve your "global" variable. You can access the MDIParent from a child, but you'll need to cast the property to your type to get your custom property.

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

  • canislupus

    Make it static. Make sure you close the connection and dispose the connection though.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    Program Manager
    Microsoft
    This post is provided "as-is"

     


  • windows form parking window

    Thank you for the info. I tried the resources. Also, I visited the link that you gave me. However, I cannot use the resources for the following 2 reasons: a) the variables defined in Properties.Resources are read-only variables, and so they cannot be changed at run time, b) Using resources, I cannot keep a live connection for my application. i.e. everytime I want to create a recordset, I have to open a new connection, which I do not want to.

  • Global Variable in Windows Application