Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Global variable in C#

Global variable in C#

Scheduled Pinned Locked Moved C#
csharpc++designregexarchitecture
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hamed Musavi
    wrote on last edited by
    #1

    In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use app.config file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.

    //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

    S M M N 4 Replies Last reply
    0
    • H Hamed Musavi

      In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use app.config file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.

      //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #2

      A global variable can be accessed by any method and a local variable can only be accessed by the method it is declared in. When a variable is declared outside of all methods it becomes global. Here is an example that shows that a local variable can only be accessed in its method and that a global variable can be accessed from any method. using System; class methods { static int MyGlobalVariable; static void ChangeGlobalValue() { int MyLocalVariable; MyLocalVariable = 4; MyGlobalVariable = 5; } public static void Main() { MyGlobalVariable = 7; ChangeGlobalValue(); } }

      Regards, Satips.

      H 1 Reply Last reply
      0
      • H Hamed Musavi

        In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use app.config file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.

        //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

        M Offline
        M Offline
        Muammar
        wrote on last edited by
        #3

        public class A
        {
        public static int globalVar;
        }

        private class B
        {
        A.globalVar = 1; //note that you dont have to create an object of class A to access your global variable.
        }


        Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

        H 1 Reply Last reply
        0
        • S Sathesh Sakthivel

          A global variable can be accessed by any method and a local variable can only be accessed by the method it is declared in. When a variable is declared outside of all methods it becomes global. Here is an example that shows that a local variable can only be accessed in its method and that a global variable can be accessed from any method. using System; class methods { static int MyGlobalVariable; static void ChangeGlobalValue() { int MyLocalVariable; MyLocalVariable = 4; MyGlobalVariable = 5; } public static void Main() { MyGlobalVariable = 7; ChangeGlobalValue(); } }

          Regards, Satips.

          H Offline
          H Offline
          Hamed Musavi
          wrote on last edited by
          #4

          What is this! I understand what's global, and what's local! I'm seeking a way to make scope of a global variable as large as my application, not only within a class. Please read questions more carefully. I provided example of what I want. I appreciate your will to help me and thank you very much, but the answer shows me that you just want to answer, regardless of the question. that made me angry in first place. Sorry!, Within your answer (**Code**) relies an answer to my question. Just the text, you know, made me a bit angry. Thanks. -- modified at 4:28 Sunday 8th April, 2007

          //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

          1 Reply Last reply
          0
          • M Muammar

            public class A
            {
            public static int globalVar;
            }

            private class B
            {
            A.globalVar = 1; //note that you dont have to create an object of class A to access your global variable.
            }


            Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

            H Offline
            H Offline
            Hamed Musavi
            wrote on last edited by
            #5

            Wow! Nice idea!:-D This is not as easy as a C++ global variable(I still need to identify owner: MyForm1.A.globalVar ), but this is a clever solution to the lack of global variables. Got my 5! Thanks. A suggestion, make class A sealed.:rose:

            //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

            G 1 Reply Last reply
            0
            • H Hamed Musavi

              In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use app.config file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.

              //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

              M Offline
              M Offline
              Mike_V
              wrote on last edited by
              #6

              What I have done is created an AppSettings class which has many public properties - example:

              sealed class AppSettings
              {
              static string appDir;

              public static string AppDirectory
              {
                  get
                  {
                      if (appDir != null)
                          return appDir;
                      appDir = (code to get app dir);
                      return appDir;
                  }
              }
              

              }

              And then I can use it from anywhere in the application. Mike

              H N 2 Replies Last reply
              0
              • M Mike_V

                What I have done is created an AppSettings class which has many public properties - example:

                sealed class AppSettings
                {
                static string appDir;

                public static string AppDirectory
                {
                    get
                    {
                        if (appDir != null)
                            return appDir;
                        appDir = (code to get app dir);
                        return appDir;
                    }
                }
                

                }

                And then I can use it from anywhere in the application. Mike

                H Offline
                H Offline
                Hamed Musavi
                wrote on last edited by
                #7

                Mike_V wrote:

                What I have done is created an AppSettings class which has many public properties

                I was thinking just about the same technic;)

                //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

                1 Reply Last reply
                0
                • H Hamed Musavi

                  Wow! Nice idea!:-D This is not as easy as a C++ global variable(I still need to identify owner: MyForm1.A.globalVar ), but this is a clever solution to the lack of global variables. Got my 5! Thanks. A suggestion, make class A sealed.:rose:

                  //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Hamed Mosavi wrote:

                  This is not as easy as a C++ global variable

                  It's a bit more verbous than using global variables, but verbosity is not bad in itself. The advantages over global variables is that it's easy to locate the variable as the code says exactly where to look, and that there is no risk of name conflicts with other modules that also uses global variables.

                  Hamed Mosavi wrote:

                  I still need to identify owner: MyForm1.A.globalVar

                  Put the class outside the form (perhaps in a class file by itself), then you only have to specify the name of the class: A.globalvar.

                  Hamed Mosavi wrote:

                  A suggestion, make class A sealed.

                  Even better, make the class static. Then you can't put non-static members in it by mistake, and it's automatically sealed. Also it's obvious how the class is supposed to be used.

                  --- single minded; short sighted; long gone;

                  1 Reply Last reply
                  0
                  • M Mike_V

                    What I have done is created an AppSettings class which has many public properties - example:

                    sealed class AppSettings
                    {
                    static string appDir;

                    public static string AppDirectory
                    {
                        get
                        {
                            if (appDir != null)
                                return appDir;
                            appDir = (code to get app dir);
                            return appDir;
                        }
                    }
                    

                    }

                    And then I can use it from anywhere in the application. Mike

                    N Offline
                    N Offline
                    Not Active
                    wrote on last edited by
                    #9

                    Mike_V wrote:

                    created an AppSettings class

                    You mean like what is already available by using app.config? :rolleyes:


                    only two letters away from being an asset

                    M 1 Reply Last reply
                    0
                    • H Hamed Musavi

                      In C++, many times I use global variables to hold values that I need in more than 1 class. As an example, during initialization process I (Programmatically) find the path to current executable module and store it in a global string variable. Or some of the options of the application that has a wide range of use, will be loaded (at RUN TIME) and used in several classes later. In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use app.config file. Is it possible to add/retrieve a variable to this file at RUN TIME? if not what sort of design pattern should I use for problems like I mentioned above? Thanks a lot in advanced.

                      //This is not a signature while (I'm_alive) { cout<<"I Love Programming"; }

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #10

                      Hamed Mosavi wrote:

                      In C#, however, I've read that there is not any global variable because it shows a poor design, instead we shall use app.config file.

                      Correct.

                      Hamed Mosavi wrote:

                      Is it possible to add/retrieve a variable to this file at RUN TIME?

                      If this is needed in your design then you may be better off considering a datastore, such as SQL Server Express or Compact Edition, both are free, lightweight and easily coded against. It would also allow for a easier upgrade path from your application, or to a full SQL Server implementation if necessary.


                      only two letters away from being an asset

                      1 Reply Last reply
                      0
                      • N Not Active

                        Mike_V wrote:

                        created an AppSettings class

                        You mean like what is already available by using app.config? :rolleyes:


                        only two letters away from being an asset

                        M Offline
                        M Offline
                        Mike_V
                        wrote on last edited by
                        #11

                        Not quite... otherwise I would have used app.config :)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups