none
How To Define Global Variable?

    Question

  • Hi,

    I want to define a global variable which can store information like Database Connection String and can be accessed accross all the forms and classes in my application. While loading the application, I would like to set this once and then use it throughout my application. How to do this?

    Also, what is the use of Resources.resx and AssemblyInfo.cs files in the application? Can they be of any use?

    Regards,
    Suman

    Thursday, September 07, 2006 4:50 AM

Answers

  • I use structs for my defines and globals. Just declare a struct like this outside any existing class but still inside the namespace:

    public struct Global
    {
        public const int aDefine;
        public int aVar;
    }

    Then when you want to use that variable or define in any class you just write a line like this:

    Global.aVar = 0;

    or

    someInt = Global.aDefine;

    Hope this helps!

    Thursday, September 07, 2006 3:43 PM

All replies

  • Hi,
    if you need to make a value what ever it was and use it from in form, then you either store it in the registry or in a file, db,.. etc

    if you don't know the way of doing this, tell me and i'll be glade to help you.

    the Resources.resx file is used to keep resources in your application to use it from there, like images, strings, or objects...

    AssemblyInfo.cs is the file that holds the application settings like version, id, and other stuff, and they are created by default and should not be removed or edited unless you know how to use them.


    Regards,

    Ahmed H. Waly

    Thursday, September 07, 2006 9:15 AM
  • Hi Ahmed,

    Thank you for your reply. Yes I am very much eager to know how to do this.

    Also please explain me how to play with Resources.resx, but I think data stored in this file is readonly and can be stored in design time only.

    Regards,
    Suman

    Thursday, September 07, 2006 2:10 PM
  • I use structs for my defines and globals. Just declare a struct like this outside any existing class but still inside the namespace:

    public struct Global
    {
        public const int aDefine;
        public int aVar;
    }

    Then when you want to use that variable or define in any class you just write a line like this:

    Global.aVar = 0;

    or

    someInt = Global.aDefine;

    Hope this helps!

    Thursday, September 07, 2006 3:43 PM